Maven:是否可以创建一个仅包含依赖项的胖 jar 和一个仅包含应用程序代码的 jar?

Maven: Is it possible to create a fat jar containing only dependencies and a jar with only application code?

我在 pom.xml 中使用了以下内容来创建 fat jar:

<plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <executions>
              <execution>
                <phase>package</phase>
                <goals>
                  <goal>single</goal>
                </goals>
              </execution>
            </executions>
            <configuration>
              <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
              </descriptorRefs>
            </configuration>
      </plugin>

但这也包括应用程序代码。我想制作 2 个 jar,一个只有应用程序代码,另一个只有依赖项。

我也试过这个:

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.5.1</version>
            <executions>
              <execution>
                <id>copy-dependencies</id>
                <phase>package</phase>
                <goals>
                <goal>copy-dependencies</goal>
                </goals>
                <configuration>
                <outputDirectory>${project.build.directory}/lib/</outputDirectory>
                </configuration>
              </execution>
            </executions>
        </plugin>

但它只是将所有依赖项 jar 放在一个文件夹中。

最简洁的方法是拥有一个多模块 maven 项目,其中 3 个项目分别完成工作,这是一个示例。

parent 把它放在一起:

<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

使用 Maven Shade Plugin,您可以生成一个包含您的依赖项的附加 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>
    <groupId>com.Whosebug.q49128604</groupId>
    <artifactId>q49128604</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.25</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.1</version>

                <executions>
                    <execution>
                        <id>dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <shadedArtifactAttached>true</shadedArtifactAttached>
                            <shadedClassifierName>dependencies</shadedClassifierName>
                            <artifactSet>
                                <excludes>
                                    <exclude>${project.groupId}:${project.artifactId}</exclude>
                                </excludes>
                            </artifactSet>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>