[英]How To: Eclipse Maven install build jar with dependencies
I am using Eclipse Maven (m2e) inside Eclipse and I am running my project like this:我在 Eclipse Eclipse 中使用 Eclipse Maven (m2e) 并且我正在这样运行我的项目:
My pom.xml
looks like this:我的
pom.xml
看起来像这样:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ro.project</groupId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>ro.project</name>
<properties>
<org.springframework.version>3.1.1.RELEASE</org.springframework.version>
<org.hibernate.version>4.1.0.Final</org.hibernate.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<classpathPrefix>lib/</classpathPrefix>
<mainClass>ro.project.ProjectServer</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<dependencies>
<dependency>
<groupId>com.sun</groupId>
<artifactId>tools</artifactId>
<version>1.7.0_02</version>
<scope>system</scope>
<systemPath>${java.home}/../lib/tools.jar</systemPath>
</dependency>
</dependencies>
<executions>
<execution>
<id>ant-magic</id>
<phase>prepare-package</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<property name="compile_classpath" refid="maven.compile.classpath" />
<property name="runtime_classpath" refid="maven.runtime.classpath" />
<property name="test_classpath" refid="maven.test.classpath" />
<property name="plugin_classpath" refid="maven.plugin.classpath" />
<ant antfile="${basedir}/clientExport.xml" target="export-all" />
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<artifactId>project-core</artifactId>
<url>http://www.project.ro</url>
</project>
After I run the maven install it is working...在我运行 maven 安装后它正在工作......
Maven run configurations: Maven 运行配置:
The problem is that my generated .jar
it doesn't have the dependencies included.... How can I configure pom.xml
to include all my dependencies in .jar
format and not unpacked.. because it seems that unpacked are not working correct...问题是我生成的
.jar
它没有包含依赖项....我如何配置pom.xml
以包含我所有的依赖.jar
格式而不是解压..因为解压似乎无法正常工作...
To be sure that including all jars is ok.. I downloaded and added each library into jar
's /lib
folder and the jar is running... so.. my only question is: How can I configure pom.xml
in order to add all my dependencies in jar
format?为确保包括所有 jars 没问题.. 我将每个库下载并添加到
jar
的/lib
文件夹中,jar 正在运行......所以..我唯一的问题是:如何配置pom.xml
以便以jar
格式添加我所有的依赖项?
I tried all methods:我尝试了所有方法:
assembly:assembly
assembly:single
assembly:single
with my descriptor (an assemble.xml
file) but it wasn't working assembly:single
与我的描述符(一个assemble.xml
文件)但它不工作maven copy dependencies
plugin but still not working with Eclipse Maven - m2e
maven copy dependencies
插件但仍然无法使用 Eclipse Maven - m2e
I am out of solutions... can anyone tell me a proper way to add my dependencies in jar?我没有解决方案...谁能告诉我在 jar 中添加依赖项的正确方法? I can't believe that
maven
is so complex and I can't find an answer to my question everywhere..我无法相信
maven
如此复杂,而且我到处都找不到问题的答案。
Thank you in advance先感谢您
There are a couple of ways of doing this.有几种方法可以做到这一点。
1) If you want an uber-jar (repacked with all dependencies), look into using and configuring the maven-shade-plugin : 1)如果你想要一个 uber-jar(重新打包所有依赖项),请查看使用和配置maven-shade-plugin :
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.group.id.Launcher1</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
This will unpack all dependencies and merge them into one JAR file.这将解压所有依赖项并将它们合并到一个 JAR 文件中。
2) If you want to deliver a bundle (zip, tar.gz, etc) with the unpacked JAR files in the bundle (perhaps under lib/) then you need to look into the maven-assembly-plugin : 2)如果你想交付一个包(zip、tar.gz 等)和包中解压的 JAR 文件(可能在 lib/ 下),那么你需要查看maven-assembly-plugin :
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.3</version>
<executions>
<execution>
<id>create-distro</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<descriptors>
<descriptor>src/main/assembly/dist.xml</descriptor>
</descriptors>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Note that this requires an assembly descriptor src/main/assembly/dist.xml
and example looks like this:请注意,这需要一个程序集描述符
src/main/assembly/dist.xml
,示例如下所示:
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0">
<id>distribution</id>
<formats>
<format>zip</format>
</formats>
<dependencySets>
<dependencySet>
<useProjectArtifact>false</useProjectArtifact>
<useTransitiveDependencies>false</useTransitiveDependencies>
<unpack>false</unpack>
<scope>runtime</scope>
<fileMode>0755</fileMode>
<directoryMode>0755</directoryMode>
<outputDirectory>bin</outputDirectory>
<includes>
<include>com.group.id:project-launch1</include>
<include>com.group.id:project-launch2</include>
</includes>
</dependencySet>
<dependencySet>
<useProjectArtifact>false</useProjectArtifact>
<useTransitiveDependencies>true</useTransitiveDependencies>
<unpack>false</unpack>
<scope>runtime</scope>
<fileMode>0644</fileMode>
<directoryMode>0755</directoryMode>
<outputDirectory>lib</outputDirectory>
<includes>
<include>com.group.id:project-lib1</include>
<include>com.group.id:project-lib2</include>
<include>com.group.id:project-lib3</include>
<include>com.group.id:project-lib4</include>
</includes>
</dependencySet>
</dependencySets>
</assembly>
And since you are now assembling dependencies, you have better define the dependency in the pom.xml, like so:由于您现在正在组装依赖项,因此最好在 pom.xml 中定义依赖项,如下所示:
<dependencies>
<dependency>
<groupId>com.group.id</groupId>
<artifactId>project-launch1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>com.group.id</groupId>
<artifactId>project-launch2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>com.group.id</groupId>
<artifactId>project-lib1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>jar</type>
</dependency>
... and so on ...
</dependencies>
3) If you are delivering a bundle with an executable JAR file launcher, you will likely need to add a maven-jar-plugin configuration in addition to the maven-assembly-plugin : 3)如果您要交付带有可执行文件 JAR 文件启动器的捆绑包,则除了maven-assembly-plugin之外,您可能还需要添加maven-jar-plugin配置:
<dependencies>
<dependency>
<groupId>com.group.id</groupId>
<artifactId>project-lib1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>com.group.id</groupId>
<artifactId>project-lib2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>com.group.id</groupId>
<artifactId>project-lib3</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>jar</type>
</dependency>
... and so on ...
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<addMavenDescriptor>false</addMavenDescriptor>
<compress>true</compress>
<manifest>
<mainClass>com.group.id.Launcher1</mainClass>
<addClasspath>true</addClasspath>
<classpathPrefix>../lib/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
Note that the "addClasspath" directive adds the project dependencies to the class path.请注意,“addClasspath”指令将项目依赖项添加到 class 路径。 This is needed for JAR launchers, as they explicitly ignore all CLASSPATH environmental variables.
这是 JAR 启动器所需要的,因为它们明确地忽略了所有 CLASSPATH 环境变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.