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

时间:2018-03-06 10:28:26

标签: maven maven-plugin maven-assembly-plugin

我在我的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放在一个文件夹中。

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

使用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>
相关问题