4

I want to build a maven jar artifact from classes. I don't have source files. These classes are originally in another artifact installed locally. I use maven-dependency-plugin to unpack the classes and put them in the target folder for this project/module.

It creates the jar.. but doesn't include the classes I just unpacked. Here's my pom:

<build>
... 

<!-- unpack myjar1.jar and myjar2.jar -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.8</version>
                <executions>
                    <execution>
                        <id>unpack</id>
                        <phase>package</phase>
                        <goals>
                            <goal>unpack</goal>
                        </goals>
                        <configuration>
                            <artifactItems>
                                <artifactItem>
                                    <groupId>com.company</groupId>
                                    <artifactId>myjar1</artifactId>
                                    <version>1.0</version>
                                    <type>jar</type>
                                    <overWrite>false</overWrite>
                                    <outputDirectory>target/final</outputDirectory>
                                </artifactItem>
                                <artifactItem>
                                    <groupId>com.company</groupId>
                                    <artifactId>myjar2</artifactId>
                                    <version>1.0</version>
                                    <type>jar</type>
                                    <overWrite>false</overWrite>
                                    <outputDirectory>target/final</outputDirectory>
                                </artifactItem>
                            </artifactItems>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <executions>
                    <execution>
                        <id>default</id>
                        <phase>package</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                        <configuration>
                            <classesDirectory>/path/to/target/final/folder</classesDirectory>
                            <includes>
                                <include>**</include>
                            </includes>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

</plugins>

</build>

How can I include these classes into my final.jar?

4

3 回答 3

3

我认为最好的解决方案是maven-shade-plugin:创建一个 pom.xml,将这 2 个库添加为依赖项并配置 maven-shade-plugin。运行mvn package,你就有了你的合并项目。

于 2013-10-07T20:10:47.410 回答
2

罗伯特上面写的也可能是一个可行的解决方案。但我想出了一个不同的出路。我只是在 maven-jar-plugin 中删除<includes>了它并且它起作用了。我通过创建构建配置并选择“调试”选项在 Eclipse 中运行构建。它吐出了很多关于“配置”的信息,否则不会显示。

谢谢!

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <executions>
                    <execution>
                        <id>default</id>
                        <phase>package</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                        <configuration>
                            <classesDirectory>path/to/final/folder</classesDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

这行得通!

于 2013-10-08T14:03:10.643 回答
2

另一种方法是将 ouputDirectory 设置为常规目标/类目录。目标/类

这样解压的类加上您的项目类将在目标/类中可用,可以通过指定使用常规 maven-jar-plugin 捆绑到 .jar 中**

完成pom:

    <build>
<plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>2.8</version>
            <executions>
                <execution>
                    <id>unpack</id>
                    <phase>package</phase>
                    <goals>
                        <goal>unpack</goal>
                    </goals>
                    <configuration>
                        <artifactItems>
                            <artifactItem>
                                <groupId>a.b.c</groupId>
                                <artifactId>aaa</artifactId>
                                <type>jar</type>
                                <overWrite>true</overWrite>
                                <outputDirectory>target/classes</outputDirectory>                                    
                            </artifactItem>
                        </artifactItems>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>2.4</version>
            <executions>
                <execution>
                    <id>default</id>
                    <phase>package</phase>
                    <goals>
                        <goal>jar</goal>
                    </goals>

                    <configuration>
            <includes>
                    <include>**</include>
            </includes>
            </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
于 2014-04-09T18:13:53.740 回答