【问题标题】:unpack dependency and repack classes using maven?使用 maven 解压缩依赖项并重新打包类?
【发布时间】:2012-10-30 15:36:30
【问题描述】:

我正在尝试解压缩一个 maven 工件 A 并将其重新打包到 maven 项目 B 中的一个新 jar 文件中。

将类文件从工件 A 解压到:

    <my.classes.folder>${project.build.directory}/staging</my.classes.folder>

使用它可以正常工作:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
                <execution>
                    <id>unpack</id>
                    <phase>generate-resources</phase>
                    <goals>
                        <goal>unpack</goal>
                    </goals>
                    <configuration>
                        <artifactItems>
                            <artifactItem>
                                <groupId>com.test</groupId>
                                <artifactId>mvn-sample</artifactId>
                                <version>1.0.0-SNAPSHOT</version>
                                <type>jar</type>
                                <overWrite>true</overWrite>
                                <outputDirectory>${my.classes.folder}</outputDirectory>
                                <includes>**/*.class,**/*.xml</includes>
                            </artifactItem>
                        </artifactItems>
                    </configuration>
                </execution>
            </executions>
        </plugin>

在同一个 pom 中,我现在想生成一个额外的 jar,其中包含刚刚解包的类:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>jar</goal>
                    </goals>
                    <configuration>
                    <classesdirectory>${my.classes.folder}</classesdirectory>
                        <classifier>sample</classifier>
                    </configuration>
                </execution>
            </executions>
        </plugin>

创建了一个新的 jar,但它不包含来自以下位置的类:

 ${my.classes.folder}

它只是默认项目 jar 的副本。有什么想法吗?

我已尝试遵循本指南:

http://jkrishnaraotech.blogspot.dk/2011/06/unpack-remove-some-classes-and-repack.html

但它不起作用。

【问题讨论】:

    标签: maven jar


    【解决方案1】:

    我建议您改用maven-shade-plugin

    这将使 unpack-repack 在同一次调用中进行。

    例如,您可以这样做:

    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>2.0</version>
        <executions>
          <execution>
            <phase>package</phase>
            <goals>
              <goal>shade</goal>
            </goals>
            <configuration>
              <filters>
                <filter>
                  <artifact>com.test:mvn-sample:1.0.0-SNAPSHOT</artifact>
                  <includes>
                    <include>**/*.class</include>
                    <include>**/*.xml</include>
                  </includes>
                </filter>
              </filters>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
    

    【讨论】:

      【解决方案2】:

      在您拥有&lt;classesdirectory&gt; 的示例中,docs 的元素为&lt;classesDirectory&gt;。我认为区分大小写很重要。

      【讨论】:

      • 也就是说,我认为@maba 的回答可能更适合这个用例。
      • 那里很好看!为此 +1。
      猜你喜欢
      • 2017-02-03
      • 1970-01-01
      • 1970-01-01
      • 2017-11-15
      • 2017-11-17
      • 1970-01-01
      • 1970-01-01
      • 2020-01-14
      • 1970-01-01
      相关资源
      最近更新 更多