【问题标题】:Maven: Is it possible to create a fat jar containing only dependencies and a jar with only application code?Maven:是否可以创建仅包含依赖项的胖 jar 和仅包含应用程序代码的 jar?
【发布时间】:2018-03-06 10:28:26
【问题描述】:

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

<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 个罐子,一个只有应用程序代码,另一个只有依赖项。

我也试过这个:

<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 maven-plugin maven-assembly-plugin


【解决方案1】:

最简洁的方法是创建一个多模块 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

【讨论】:

  • 第一个和第二个项目执行的操作有什么区别?我知道第三个项目将构建包含所有依赖项的 jar。
  • 看不懂上面common项目的使用
  • 普通项目是你的代码,其他2个项目是构建两个不同的胖罐子。
  • 如果common项目包含应用程序代码,那么为什么其他2个项目包含它作为maven dependency?我的意思是目标是构建一个没有依赖关系的应用程序 jar 和一个只有依赖关系的胖 jar
  • 该示例完全按照您的意愿进行操作,一个项目用于代码,2 个项目用于制作胖罐子,这是 Maven 的正常使用。我不知道如何让它变得更容易。
【解决方案2】:

使用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.stackoverflow.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>

【讨论】:

    猜你喜欢
    • 2016-06-04
    • 2020-04-08
    • 2014-10-06
    • 2012-05-13
    • 1970-01-01
    • 1970-01-01
    • 2014-06-08
    • 2013-03-29
    • 2017-02-01
    相关资源
    最近更新 更多