Heroku – 使用本地依赖的Maven应用在本地运行正常,但无法部署。

huangapple go评论79阅读模式
英文:

Heroku - Maven app with local dependencies works perfect in local but is not deploying

问题

我有一个在Heroku上的应用程序,其中包含一些本地依赖项,我通过以下命令进行了安装:

mvn install:install-file -Dfile="lib\com\sforce\soap\partner.0\partner-1.0.jar" -DgroupId="com.sforce.soap.partner" -DartifactId=partner -Dversion="1.0" -Dpackaging=jar

通过这个命令,我执行了mvn packageclean install,在我的本地环境中,我的应用程序运行得很完美。但是,当我尝试将这个应用程序部署到Heroku时,出现了“无法解析依赖项错误”(特别是在partner.jar的依赖项上失败)。我尝试在Heroku控制台中使用上述命令安装我的JAR文件,但是它显示找不到mvn命令。

我该怎么解决这个问题?以下是我的POM文件:

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>org.aisfg</groupId>
	<artifactId>ais-salesforce-heroku-bloomber</artifactId>
	<version>1.0</version>

	<build>
		<plugins>
			<plugin> 
				<groupId>org.codehaus.mojo</groupId>
				<artifactId>appassembler-maven-plugin</artifactId>
				<version>1.1.1</version>
				<configuration>
					<assembleDirectory>target</assembleDirectory>
					<programs>
						<program>
							<mainClass>Service</mainClass>
							<name>service</name>
						</program>
					</programs>
				</configuration>
				<executions>
					<execution>
						<phase>package</phase>
						<goals>
							<goal>assemble</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
	<dependencies>
		<!-- https://mvnrepository.com/artifact/com.force.api/force-wsc -->
		<dependency>
			<groupId>com.force.api</groupId>
			<artifactId>force-wsc</artifactId>
			<version>35.0.0</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.mozilla/rhino -->
		<dependency>
			<groupId>org.mozilla</groupId>
			<artifactId>rhino</artifactId>
			<version>1.7.12</version>
		</dependency>
		<!-- https://www.stringtemplate.org/ -->
		<dependency>
			<groupId>org.antlr</groupId>
			<artifactId>ST4</artifactId>
			<version>4.0.8</version>
		</dependency>
		<dependency>
			<groupId>com.google.code.gson</groupId>
			<artifactId>gson</artifactId>
			<version>2.8.6</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.json/json -->
		<dependency>
			<groupId>org.json</groupId>
			<artifactId>json</artifactId>
			<version>20200518</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpclient</artifactId>
			<version>4.5.12</version>
		</dependency>

        <!-- 本地依赖项 -->
		<dependency> 
			<groupId>com.bloomberglp.blpapi</groupId>
			<artifactId>blpapi</artifactId>
			<version>3.12.1</version>
		</dependency>
		<dependency>
			<groupId>com.sforce.soap.partner</groupId>
			<artifactId>partner</artifactId>
			<version>1.0</version>
		</dependency>
		<dependency>
			<groupId>com.sforce.soap.enterprise</groupId>
			<artifactId>enterprise</artifactId>
			<version>1.0</version> 
		</dependency>
	</dependencies> 
</project>
英文:

I have an Heroku app with some local dependencies, which I have installed by the following command:

mvn install:install-file -Dfile=&quot;lib\com\sforce\soap\partner\1.0\partner-1.0.jar&quot; -DgroupId=&quot;com.sforce.soap.partner&quot; -DartifactId=partner -Dversion=&quot;1.0&quot; -Dpackaging=jar

With this I do a mvn package and a clean install, and my app works perfect in my local environment. The problem that I have is when I try to deploy this app to Heroku, because it fails with "Could not resolve dependencies error" (especially fails in partner.jar dependency). I have tried to install my jars in the heroku console with the previous command but it says that mvn command is not found.

What can I do to solve this? This is my pom:

&lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot;
xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
&lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
&lt;groupId&gt;org.aisfg&lt;/groupId&gt;
&lt;artifactId&gt;ais-salesforce-heroku-bloomber&lt;/artifactId&gt;
&lt;version&gt;1.0&lt;/version&gt;
&lt;build&gt;
&lt;plugins&gt;
&lt;plugin&gt; 
&lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
&lt;artifactId&gt;appassembler-maven-plugin&lt;/artifactId&gt;
&lt;version&gt;1.1.1&lt;/version&gt;
&lt;configuration&gt;
&lt;assembleDirectory&gt;target&lt;/assembleDirectory&gt;
&lt;programs&gt;
&lt;program&gt;
&lt;mainClass&gt;Service&lt;/mainClass&gt;
&lt;name&gt;service&lt;/name&gt;
&lt;/program&gt;
&lt;/programs&gt;
&lt;/configuration&gt;
&lt;executions&gt;
&lt;execution&gt;
&lt;phase&gt;package&lt;/phase&gt;
&lt;goals&gt;
&lt;goal&gt;assemble&lt;/goal&gt;
&lt;/goals&gt;
&lt;/execution&gt;
&lt;/executions&gt;
&lt;/plugin&gt;
&lt;/plugins&gt;
&lt;/build&gt;
&lt;dependencies&gt;
&lt;!-- https://mvnrepository.com/artifact/com.force.api/force-wsc --&gt;
&lt;dependency&gt;
&lt;groupId&gt;com.force.api&lt;/groupId&gt;
&lt;artifactId&gt;force-wsc&lt;/artifactId&gt;
&lt;version&gt;35.0.0&lt;/version&gt;
&lt;/dependency&gt;
&lt;!-- https://mvnrepository.com/artifact/org.mozilla/rhino --&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.mozilla&lt;/groupId&gt;
&lt;artifactId&gt;rhino&lt;/artifactId&gt;
&lt;version&gt;1.7.12&lt;/version&gt;
&lt;/dependency&gt;
&lt;!-- https://www.stringtemplate.org/ --&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.antlr&lt;/groupId&gt;
&lt;artifactId&gt;ST4&lt;/artifactId&gt;
&lt;version&gt;4.0.8&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;com.google.code.gson&lt;/groupId&gt;
&lt;artifactId&gt;gson&lt;/artifactId&gt;
&lt;version&gt;2.8.6&lt;/version&gt;
&lt;/dependency&gt;
&lt;!-- https://mvnrepository.com/artifact/org.json/json --&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.json&lt;/groupId&gt;
&lt;artifactId&gt;json&lt;/artifactId&gt;
&lt;version&gt;20200518&lt;/version&gt;
&lt;/dependency&gt;
&lt;!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient --&gt;
&lt;dependency&gt;
&lt;groupId&gt;org.apache.httpcomponents&lt;/groupId&gt;
&lt;artifactId&gt;httpclient&lt;/artifactId&gt;
&lt;version&gt;4.5.12&lt;/version&gt;
&lt;/dependency&gt;
&lt;!-- LOCAL DEPENDENCIES --&gt;
&lt;dependency&gt; 
&lt;groupId&gt;com.bloomberglp.blpapi&lt;/groupId&gt;
&lt;artifactId&gt;blpapi&lt;/artifactId&gt;
&lt;version&gt;3.12.1&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;com.sforce.soap.partner&lt;/groupId&gt;
&lt;artifactId&gt;partner&lt;/artifactId&gt;
&lt;version&gt;1.0&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
&lt;groupId&gt;com.sforce.soap.enterprise&lt;/groupId&gt;
&lt;artifactId&gt;enterprise&lt;/artifactId&gt;
&lt;version&gt;1.0&lt;/version&gt; 
&lt;/dependency&gt;
&lt;/dependencies&gt; 
&lt;!-- &lt;repositories&gt;
&lt;repository&gt;
&lt;id&gt;project_lib&lt;/id&gt;
&lt;name&gt;Repository in project&#39;s lib dir&lt;/name&gt;
&lt;layout&gt;default&lt;/layout&gt;
&lt;url&gt;file:///${project.basedir}/lib&lt;/url&gt; 
&lt;/repository&gt;
&lt;/repositories&gt; --&gt;

</project>

答案1

得分: 4

mvn install:install-file将依赖项(默认情况下)安装在本地存储库中(在Linux/macOS下默认为~/.m2/repository)。这将使Maven在您的本地计算机上可以使用该依赖项,但在Heroku的构建过程中不可用,正如您所观察到的。

在Herkou控制台中使用此命令也不会起作用(如果mvn可用的话),原因有几个:

但是,您可以使其工作。这将需要将您的JAR文件检入Git,以便Heroku在构建期间访问它。这有一个缺点:如果您经常更新文件,您的存储库大小将迅速增长,因为Git不能再仅存储版本之间的增量。然而,我认为在这种情况下,这似乎是一个合理的用例。

  1. 在您的pom.xml中添加一个指向项目目录中某个目录的新存储库:
<repositories>
    <repository>
        <id>local-maven-repository</id>
        <url>file:///${project.basedir}/maven-repository</url>
    </repository>
</repositories>
  1. 创建该目录(确保您在项目目录中运行所有命令):
mkdir maven-repository
  1. 将您的构件部署到该存储库:
mvn deploy:deploy-file -DgroupId=com.sforce.soap.partner -DartifactId=partner -Dversion=1.0 -Durl=file:./maven-repository/ -DrepositoryId=local-maven-repository -DupdateReleaseInfo=true -Dfile=lib\com\sforce\soap\partner\1.0\partner-1.0.jar
  1. 将更改和依赖的JAR文件添加到Git,并部署到Heroku:
git add .
git commit -m "Add local dependency"
git push heroku master

如果您不想检入二进制文件,您将不得不使用远程访问的Nexus存储库来存储您的依赖项。您可以使用密码保护它,并在Heroku上进行设置:https://devcenter.heroku.com/articles/using-a-custom-maven-settings-xml#using-password-protected-repositories

英文:

mvn install:install-file installs the dependency (by default) in your local repository (defaults to ~/.m2/repository under Linux/macOS). This will make the dependency available to Maven on your local machine, but not during the build process in Heroku as you observed.

Using this command in the Herkou console would also (if mvn would be available) not work for a couple of reasons:

However, you can make this work. This will require checking in your JAR file to Git so Heroku has access to it during the build. This has a drawback: if you update the file often, your repo size will grow quickly since Git can no longer only store deltas between versions. However, I think this seems to be a reasonable use-case here.

  1. Add a new repository to your pom.xml that points to a directory in your project directory:
&lt;repositories&gt;
&lt;repository&gt;
&lt;id&gt;local-maven-repository&lt;/id&gt;
&lt;url&gt;file:///${project.basedir}/maven-repository&lt;/url&gt;
&lt;/repository&gt;
&lt;/repositories&gt;
  1. Create that directory (make sure you're running all commands while you're in your project directory):

mkdir maven-repository

  1. Deploy your artifact to that repository:
mvn deploy:deploy-file -DgroupId=com.sforce.soap.partner -DartifactId=partner -Dversion=1.0 -Durl=file:./maven-repository/ -DrepositoryId=local-maven-repository -DupdateReleaseInfo=true -Dfile=lib\com\sforce\soap\partner\1.0\partner-1.0.jar
  1. Add your changes and dependency JARs to Git and deploy to Heroku:
git add .
git commit -m &quot;Add local dependency&quot;
git push heroku master

If you don't want to check-in your binary, you'll have to have a remotely accessible Nexus repo with your dependency. You can protect it with a password and set it up on Heroku: https://devcenter.heroku.com/articles/using-a-custom-maven-settings-xml#using-password-protected-repositories

huangapple
  • 本文由 发表于 2020年9月8日 20:35:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/63794086.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定