最简洁的方法是创建一个多模块 maven 项目,其中 3 个项目分别工作,这是一个示例。
父母将这一切放在一起:
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.greg</groupId>
<artifactId>two-fat-jars</artifactId>
<version>1.0</version>
<packaging>pom</packaging>
<name>two-fat-jars</name>
<url>http://maven.apache.org</url>
<modules>
<module>common</module>
<module>application-jar</module>
<module>dependencies-jar</module>
</modules>
</project>
第一个项目构建代码
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.greg</groupId>
<artifactId>two-fat-jars</artifactId>
<version>1.0</version>
</parent>
<artifactId>common</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
</dependency>
</dependencies>
</project>
第二个项目对普通项目有依赖,构建应用jar,排除某些依赖:
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.greg</groupId>
<artifactId>two-fat-jars</artifactId>
<version>1.0</version>
</parent>
<artifactId>application-jar</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.greg</groupId>
<artifactId>common</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<excludes>
<exclude>junit:junit</exclude>
</excludes>
<includes>
<include>com.greg:common</include>
</includes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
第三个项目通过排除应用代码依赖来构建依赖jar:
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.greg</groupId>
<artifactId>two-fat-jars</artifactId>
<version>1.0</version>
</parent>
<artifactId>dependencies-jar</artifactId>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>com.greg</groupId>
<artifactId>common</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<excludes>
<exclude>com.greg:common</exclude>
</excludes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
这是示例代码结构:
./pom.xml
./application-jar
./application-jar/pom.xml
./application-jar/dependency-reduced-pom.xml
./dependencies-jar
./dependencies-jar/pom.xml
./common
./common/src
./common/src/main
./common/src/main/java
./common/src/main/java/com
./common/src/main/java/com/greg
./common/src/main/java/com/greg/App.java
./common/src/main/resources
./common/src/main/resources/configs
./common/src/main/resources/configs/config1.xml
./common/src/main/resources/configs/config2.xml
./common/src/main/resources/test2.properties
./common/src/main/resources/test1.properties
./common/src/test
./common/src/test/java
./common/src/test/java/com
./common/src/test/java/com/greg
./common/src/test/java/com/greg/AppTest.java
./common/pom.xml
./common/configs
./common/configs/config1.xml
./common/configs/config2.xml