Java 使用Maven在单独的JAR中构建并打包应用程序和依赖项
我正在寻找一个pom.xml配置,它可以将我的应用程序打包到一个jar中,并将所有应用程序依赖项打包到另一个jar中。我查看了maven assembly插件,通过以下配置,我能够构建一个应用程序jar,然后构建一个包含所有依赖项的应用程序jar,但我需要依赖项jar不包含我的应用程序:Java 使用Maven在单独的JAR中构建并打包应用程序和依赖项,java,maven,plugins,Java,Maven,Plugins,我正在寻找一个pom.xml配置,它可以将我的应用程序打包到一个jar中,并将所有应用程序依赖项打包到另一个jar中。我查看了maven assembly插件,通过以下配置,我能够构建一个应用程序jar,然后构建一个包含所有依赖项的应用程序jar,但我需要依赖项jar不包含我的应用程序: <plugin> <artifactId>maven-assembly-plugin</artifactId> <version>2.4</
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<finalName>${project.artifactId}-${project.version}</finalName>
<appendAssemblyId>false</appendAssemblyId>
</configuration>
<executions>
<execution>
<id>jar-with-dependencies</id>
<phase>package</phase>
<goals>
<goal>attached</goal>
</goals>
</execution>
</executions>
</plugin>
maven汇编插件
2.4
带有依赖项的jar
${project.artifactId}-${project.version}
假的
带有依赖项的jar
包裹
附属的
谢谢我已经看到通过在模块中构建应用程序解决了这个问题。这里有一个很好的博客: 对于您的情况,您可以有一个名为“dependencies”的模块,该模块使用maven dependency插件和maven assembly插件复制所有依赖项,并将它们打包到一个jar中。然后,您的应用程序可以在单独的模块中引用该jar作为其依赖项。顶层将同时构建两者 顶级pom
.
.
.
<packaging>pom</packaging>
.
.
<modules>
<module>dependencies</module>
<module>yourApp</module>
</modules>
.
.
.
.
.
.
<dependencies>
<dependency>
<groupId>some.group.id</groupId>
<artifactId>yourFirstDependency</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>some.group.id2</groupId>
<artifactId>yourSecondDependency</artifactId>
<version>1.1.4</version>
</dependency>
<dependencies>
<build>
<defaultGoal>install</defaultGoal>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
.
.
.
.
.
.
<!-- Reference your dependency module here as the first in the list -->
<dependencies>
<dependency>
<groupId>your.group.id</groupId>
<artifactId>yourDependencyJarName</artifactId>
<version>1.0</version>
</dependency>
.
.
</dependencies>
.
.
.
。
.
.
聚甲醛
.
.
依赖关系
你的应用程序
.
.
.
依赖项pom
.
.
.
<packaging>pom</packaging>
.
.
<modules>
<module>dependencies</module>
<module>yourApp</module>
</modules>
.
.
.
.
.
.
<dependencies>
<dependency>
<groupId>some.group.id</groupId>
<artifactId>yourFirstDependency</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>some.group.id2</groupId>
<artifactId>yourSecondDependency</artifactId>
<version>1.1.4</version>
</dependency>
<dependencies>
<build>
<defaultGoal>install</defaultGoal>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
.
.
.
.
.
.
<!-- Reference your dependency module here as the first in the list -->
<dependencies>
<dependency>
<groupId>your.group.id</groupId>
<artifactId>yourDependencyJarName</artifactId>
<version>1.0</version>
</dependency>
.
.
</dependencies>
.
.
.
。
.
.
some.group.id
你的第一依赖
1
some.group.id2
你的第二依赖
1.1.4
安装
${project.artifactId}
org.apache.maven.plugins
maven依赖插件
包裹
复制依赖项
${project.build.directory}
org.apache.maven.plugins
maven汇编插件
带有依赖项的jar
组装
包裹
单一的
.
.
.
主应用程序pom
.
.
.
<packaging>pom</packaging>
.
.
<modules>
<module>dependencies</module>
<module>yourApp</module>
</modules>
.
.
.
.
.
.
<dependencies>
<dependency>
<groupId>some.group.id</groupId>
<artifactId>yourFirstDependency</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>some.group.id2</groupId>
<artifactId>yourSecondDependency</artifactId>
<version>1.1.4</version>
</dependency>
<dependencies>
<build>
<defaultGoal>install</defaultGoal>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
.
.
.
.
.
.
<!-- Reference your dependency module here as the first in the list -->
<dependencies>
<dependency>
<groupId>your.group.id</groupId>
<artifactId>yourDependencyJarName</artifactId>
<version>1.0</version>
</dependency>
.
.
</dependencies>
.
.
.
。
.
.
您的.group.id
你的依赖名
1
.
.
.
.
.