我创建了一个 C/S 框架作为多模块 Maven 项目。它包含三个模块:“server”、“client”和“common”。“公共”模块中的类被“服务器”和“客户端”使用。
但是我不想要一个独立的common.jar
. 相反,我希望将“common”模块中的类直接编译成server.jar
and client.jar
. 有什么办法可以做到吗?
我创建了一个 C/S 框架作为多模块 Maven 项目。它包含三个模块:“server”、“client”和“common”。“公共”模块中的类被“服务器”和“客户端”使用。
但是我不想要一个独立的common.jar
. 相反,我希望将“common”模块中的类直接编译成server.jar
and client.jar
. 有什么办法可以做到吗?
您可以使用maven 依赖插件将公共 jar解压到其他项目中。
完成此操作的最佳方法是使用maven-shade-plugin
.
将此添加到您的服务器/客户端 pom:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>1.7.1</version>
<configuration>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
</project>
您最终会得到与/common
相同的 jar 中的类。server
client
使用带有预定义描述符 jar-with-dependencies 的 maven 程序集插件
选项 1: 在“服务器”项目 pom.xml 中包含以下内容:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.3</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
如果您有的话,这也将包括其他第三方依赖项。
选项 2: 此选项用于排除第三方库(如果有)。
1.在pom.xml同目录下创建一个assembly.xml如下。
<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>custom</id>
<formats>
<format>jar</format>
</formats>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>runtime</scope>
<includes>
<include>common</include>
</includes>
</dependencySet>
</dependencySets>
</assembly>
包含标签必须包含<groupId>:<artifactId>
格式,这里只提到了 artifactId 'common',因为我不知道你的 groupId。
2.在“服务器”pom.xml 中包含以下内容:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.3</version>
<configuration>
<descriptors>
<descriptor>assembly.xml</descriptor>
</descriptors>
</configuration>
</plugin>
</plugins>
</build>
运行程序集命令(程序集:单个)。