[英]How to include only one dependency project classes in final jar
I use Maven to build my MyProject
. 我使用Maven构建MyProject
。 There are several other projects which are used as library projects for ' MyProject
'. 还有其他几个项目用作“ MyProject
”的库项目。
In pom.xml of MyProject
, I defined those library projects as dependencies of MyProject
. 在MyProject
pom.xml中,我将这些库项目定义为MyProject
依赖项。 One of the Library project is named " OneLibProject
": Library项目之一被命名为“ OneLibProject
”:
<dependency>
<groupId>com.xxx.OneLibProject</groupId>
<artifactId>OneLibProject</artifactId>
<version>1.0.0-SNAPSHOT</version>
<type>jar</type>
</dependency>
<dependency>
...
</dependency>
Under MyProject
root path, after I run : maven clean install
a MyProject.jar is generated. 在MyProject
根路径下,运行以下命令: maven clean install
会生成MyProject.jar 。 But those classes defined in library projects(dependencies) are not included in this MyProject.jar . 但是在库项目(依赖项) 中定义的那些类未包含在此MyProject.jar中 。
Each library project also have its own pom & can generate its own jar. 每个图书馆项目也都有自己的pom并可以生成自己的jar。
Now, what I want to do is I want to have my pom.xml in " MyProject
" to be configured so that the generated MyProject.jar file contains the classes of OneLibProject
and classes of MyProject
. 现在,我想做的是在“ MyProject
”中配置我的pom.xml ,以便生成的MyProject.jar文件包含OneLibProject
类和MyProject
类。 Other library project classes are not included . 不包括其他图书馆项目类 。
How to achieve this? 如何实现呢?
You can use maven-assembly-plugin and configure which dependencies to include in jar in assembly descriptor's dependencySet section. 您可以使用maven-assembly-plugin并在程序集描述符的dependencySet部分中配置要包含在jar中的依赖项。
Assuming you have project y and z. 假设您有项目y和z。 Add this plugin to y's pom 将此插件添加到y的pom中
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4</version>
<configuration>
<descriptor>src/main/assembly/assembly.xml</descriptor>
</configuration>
</plugin>
place this assembly.xml in scr/main/assembly 将此Assembly.xml放在scr / main / assembly中
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>jar-with-dependencies</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>runtime</scope>
<includes>
<include>test:y</include>
<include>test:z</include>
</includes>
</dependencySet>
</dependencySets>
</assembly>
run mvn install
in z 在z中运行mvn install
run mvn package assembly:assembly in y 在y中运行mvn package assembly:assembly
if all correct you will get y-0.0.1-SNAPSHOT-jar-with-dependencies.jar in target folder. 如果一切正确,您将在目标文件夹中获得y-0.0.1-SNAPSHOT-jar-with-dependencies.jar。 It will contain y and x classes only. 它仅包含y和x类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.