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

时间:2017-07-10 18:55:11

标签: maven maven-shade-plugin maven-dependency-plugin

我有一个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-fat.jar但是Foo生成了一个简单的jar foo.jar,它不包含依赖项。可能需要注意的是,Foo在本地仓库中唯一的工件是纤薄的罐子,我无法通过运行任何Maven生命周期来获得胖罐。在Foo中直接运行mvn clean package会打包一个胖jar,但不会在Bar的pom.xml中调用,如上所示。

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

2 个答案:

答案 0 :(得分:3)

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 中为了包含胖子Foo类,你需要使用 maven-dependency-plugin 复制目标并确保使用着色分类器

注释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>

答案 1 :(得分:2)

Re:&#34; 这个[Bar] [...]让Foo生成一个简单的jar foo.jar &#34; - 你确定这不是以前的Foo版本吗?由于dependency:copy只是:

  

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

和re:&#34; 当[Foo]从Bar的pom.xml调用时,如上所示&#34; - 你不能调用&#34;,即从另一个项目运行项目的构建(但是从聚合器/多模块项目或使用Maven Invoker Plugin或Groovy或Ant运行脚本等。)

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

在Foo的dev阶段使用install:install-file,让... # Start a new feature branch #hg update dev #hg update release hg branch dev # <--- changed hg branch feature-02 ... 成为真正的&#34;本地仓库中的工件,并在Bar的abort: a branch of the same name already exists声明中使用。

相关问题