首页 文章

使用maven-dependency-plugin复制着色的工件

提问于
浏览
1

我有一个 Maven 项目 Foo ,它使用maven-shade-plugin打包一个webstart胖jar,包含所有依赖项 .

<plugin>
    <artifactId>maven-shade-plugin</artifactId>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <finalName>foo-fat</finalName>
                ...

另一个项目是 Bar ,这是一个Web应用程序,除其他外,分发Foo webstart . 我需要将foo-fat.jar复制到Bar临时预包目录中,为此,我使用maven-dependency-plugin

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>copy</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>copy</goal>
            </goals>
            <configuration>
                <artifactItems>
                    <artifactItem>
                        <groupId>my-group</groupId>
                        <artifactId>foo</artifactId>
                        <outputDirectory>...</outputDirectory>                  
                    </artifactItem>
                </artifactItems>
            </configuration>
        </execution>
    </executions>
</plugin>

问题是,这不包括依赖关系 . 可能需要注意的是,Foo在本地仓库中唯一的工件是纤薄的 jar ,我无法通过运行任何Maven生命周期来获得胖 jar . 直接在Foo中运行 mvn clean package 会打包一个胖jar但是从Bar的pom.xml调用时不会如上所示 .

如何让maven-dependency-plugin使用不同的项目工件,例如shade插件制作的胖 jar ?

2 回答

  • 2

    Re:“这个[Bar] [...]有Foo生成一个简单的jar foo.jar” - 你确定这不是来自之前构建的Foo吗?自dependency:copy以来:

    ...将工件库列表从存储库复制到已定义的位置

    并重新:“当[Foo]从Bar的pom.xml调用时,如上所示" – You can't "调用”,即运行一个项目的构建,来自另一个项目(但来自聚合器/多模块项目或Maven Invoker Plugin或Groovy)或Ant脚本等) .


    现在回答的想法(没有尝试过):

    在Foo的 install 阶段使用install:install-file在您的本地仓库中制作 foo-fat.jar "real"工件,并在Bar的 <artifactItem> 声明中使用它 .

  • 2

    为了 Foo class 能够在使用maven install或maven包时生成 fat jar ,你需要使用maven-shade-plugin,你已经在做了,但关键的一步是确保在其配置中使用shadedClassifierName . 例如:

    <artifactId>maven-shade-plugin</artifactId>
      <executions>
        <execution>
          <phase>package</phase>
          <goals>
            <goal>shade</goal>
          </goals>
          <configuration>
            <shadedClassifierName>shaded</shadedClassifierName>
            <shadedArtifactAttached>true</shadedArtifactAttached>
          </configuration>
        </execution>
      </executions>
    

    然后在 Bar class 中为了包含 fat jar of Foo class ,您将需要使用带复制目标的 maven-dependency-plugin 并确保使用 shaded classifier 注释artifactItem

    例如:

    <plugin>
      <artifactId>maven-dependency-plugin</artifactId>
        <executions>
          <execution>
            <goals>
              <goal>copy</goal>
            </goals>
            <configuration>           
              <outputDirectory>${project.build.directory}/temp</outputDirectory>
              <artifactItems>
                <artifactItem>
                  <groupId>...</groupId>
                  <artifactId>...</artifactId>
                  <version>${project.version}</version>
                  <type>jar</type>
                  <classifier>shaded</classifier>
                </artifactItem>
              </artifactItems>
            </configuration> 
          </execution>
        </executions>
    </plugin>
    

相关问题