英文:
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 package
和clean 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="lib\com\sforce\soap\partner\1.0\partner-1.0.jar" -DgroupId="com.sforce.soap.partner" -DartifactId=partner -Dversion="1.0" -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:
<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>
<!-- LOCAL DEPENDENCIES -->
<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>
<!-- <repositories>
<repository>
<id>project_lib</id>
<name>Repository in project's lib dir</name>
<layout>default</layout>
<url>file:///${project.basedir}/lib</url>
</repository>
</repositories> -->
</project>
答案1
得分: 4
mvn install:install-file
将依赖项(默认情况下)安装在本地存储库中(在Linux/macOS下默认为~/.m2/repository
)。这将使Maven在您的本地计算机上可以使用该依赖项,但在Heroku的构建过程中不可用,正如您所观察到的。
在Herkou控制台中使用此命令也不会起作用(如果mvn
可用的话),原因有几个:
- Heroku Dynos的文件系统是临时的
- 控制台在您的已编译应用程序上运行,而不是在构建系统上运行
但是,您可以使其工作。这将需要将您的JAR文件检入Git,以便Heroku在构建期间访问它。这有一个缺点:如果您经常更新文件,您的存储库大小将迅速增长,因为Git不能再仅存储版本之间的增量。然而,我认为在这种情况下,这似乎是一个合理的用例。
- 在您的
pom.xml
中添加一个指向项目目录中某个目录的新存储库:
<repositories>
<repository>
<id>local-maven-repository</id>
<url>file:///${project.basedir}/maven-repository</url>
</repository>
</repositories>
- 创建该目录(确保您在项目目录中运行所有命令):
mkdir maven-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
- 将更改和依赖的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:
- The file system of Heroku Dynos is ephemeral
- The console runs for your compiled application, not for the build system
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.
- Add a new repository to your
pom.xml
that points to a directory in your project directory:
<repositories>
<repository>
<id>local-maven-repository</id>
<url>file:///${project.basedir}/maven-repository</url>
</repository>
</repositories>
- Create that directory (make sure you're running all commands while you're in your project directory):
mkdir maven-repository
- 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
- Add your changes and dependency JARs to Git and deploy to Heroku:
git add .
git commit -m "Add local dependency"
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论