[英]How to create a Maven project jar with dependencies which are used by the buildpath?
I am trying to create a Maven project jar file. 我正在尝试创建一个Maven项目jar文件。 I know about the Maven repository and the
pom.xml
file configuration. 我了解Maven信息库和
pom.xml
文件配置。 But my query is a bit different from that. 但是我的查询与此有所不同。
I have created a Maven repository. 我已经创建了一个Maven存储库。 I have included the respective dependency jar files by downloading them and then given in the build path of the project (as I am using Eclipse Neon).
我已经下载了各自的依赖项jar文件,然后在项目的构建路径中给出了它们(就像我使用Eclipse Neon一样)。
Now when I run as --> Maven install
, it creates a jar file. 现在,当我
run as --> Maven install
时,它将创建一个jar文件。 But that jar is of the only project and not the dependencies that I have included. 但是那个jar是唯一的项目,而不是我包含的依赖项。
I have checked and got suggestions that I need to include them in the Maven pom.xml, but I do not know exactly which jar is getting used in my program. 我已经检查并获得建议,需要将它们包括在Maven pom.xml中,但是我不确切知道程序中正在使用哪个jar。
So I would like to know how can I automatically make the Maven project detect the dependencies and write them to the pom.xml
file without my interference, if possible. 因此,我想知道如何在可能的情况下自动使Maven项目检测依赖关系并将其写入
pom.xml
文件而不会受到干扰。
Kindly let me know how is this possible? 请让我知道这怎么可能?
I have solved this problem using the maven-dependency-plugin
. 我已经使用
maven-dependency-plugin
解决了这个问题。
This plugin make a copy of all dependencies needed by your jar file into the directory ${project.build.directory}/lib
. 该插件将jar文件所需的所有依赖项复制到
${project.build.directory}/lib
目录中。
When you need to start your jar file you have to specify the classpath -cp /path/to/your-jar-file.jar:/path/to/your/lib/*
当您需要启动jar文件时,必须指定类路径
-cp /path/to/your-jar-file.jar:/path/to/your/lib/*
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Another solution can be use directly Maven to start your application, in this way maven transparently add all the dependencies configured. 另一个解决方案可以直接使用Maven启动您的应用程序,以这种方式maven透明地添加所有已配置的依赖项。 You have just to take care before of that to execute the Maven
package
goal. 在执行Maven
package
目标之前,您只需要保重。
# create your jar file with maven
mvn clean package
# execute your class where is main
mvn exec:java -Dexec.mainClass="your.package.App" -Dexec.args="your app params"
You can even wrap this line into a shell script run.sh
: 您甚至可以将此行包装到shell脚本
run.sh
:
#!/bin/bash
PARAMS="$*"
if [ "A$PARAMS" == "A" ]
then
PARAMS="--help"
fi
mvn exec:java -Dexec.mainClass="your.package.App" -Dexec.args="$PARAMS"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.