3

我正在尝试构建一个胖罐子以在其他地方使用它。
maven assembly plugin为此使用:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>3.1.1</version>
    <configuration>
        <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
        </descriptorRefs>

    </configuration>
    <executions>
        <execution>
            <id>assemble-all</id>
            <phase>package</phase>
            <goals>
                <goal>single</goal>
            </goals>
        </execution>
    </executions>
</plugin>

在我的依赖项中,我有一个本地 jar 依赖项,我无法拒绝:

<dependency>
    <groupId>com.example</groupId>
    <artifactId>library</artifactId>
    <version>1.0.0</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/lib/my-jar.jar</systemPath>
</dependency>

但是这个罐子包含在我用mvn package.
将我的本地 jar 包含到 fat jar 作为依赖项的最佳方法是什么?

UPD。
有一些相关的问题,但他们并没有完全回答这个问题:
在构建 maven-assembly-plugin 之前将 jar 添加到 maven 本地存储库
中不会添加具有系统范围的依赖项

4

1 回答 1

2

更容易使用阴影插件:

父项目:

<?xml version="1.0" encoding="UTF-8"?>
<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>fat-jar</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>

  <modules>
    <module>library-jar</module>
    <module>final-jar</module>
  </modules>

</project>

图书馆项目:

<?xml version="1.0" encoding="UTF-8"?>
<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>
    <artifactId>fat-jar</artifactId>
    <groupId>com.greg</groupId>
    <version>1.0-SNAPSHOT</version>
  </parent>

  <artifactId>library-jar</artifactId>
  <dependencies>
    ....
  </dependencies>

  <build>
  </build>
</project>

在同一个父级下,依赖于库的最终 jar:

<?xml version="1.0" encoding="UTF-8"?>
<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>
        <artifactId>fat-jar</artifactId>
        <groupId>com.greg</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>final-jar</artifactId>

    <dependencies>
        <dependency>
            <groupId>com.greg</groupId>
            <artifactId>library-jar</artifactId>
            <version>${project.version}</version>
        </dependency>
          ...
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>3.2.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                        implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>com.greg.App</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
于 2019-02-05T11:38:40.340 回答