如何使用Maven 3.6.0将第三方jar复制到目标jar

时间:2019-05-09 18:28:35

标签: maven-3

我试图用Maven创建一个可执行jar,其中所有第三方依赖项jar都在根目录中。包括两个示例图片,以及我的pom.xml

有了这个pom,我得到的结果正好在罐子下面分解成文件夹的位置。

<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://   /xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.mycompany.xmlrpc</groupId>
    <artifactId>gz-xmlrpc-server</artifactId>
    <version>1.1.0-RELEASE</version>
    <packaging>jar</packaging>
    <name>gz-xmlrpc-server</name>
    <url>http://maven.apache.org</url>

    <properties>
        <java-version>1.7</java-version>
        <target-java-version>1.7</target-java-version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.maven.surefire/surefire-junit3 -->
        <dependency>
            <groupId>org.apache.maven.surefire</groupId>
            <artifactId>surefire-junit3</artifactId>
            <version>3.0.0-M3</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.xmlrpc/xmlrpc-server -->
        <dependency>
            <groupId>org.apache.xmlrpc</groupId>
            <artifactId>xmlrpc-server</artifactId>
            <version>3.1.3</version>
        </dependency>

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.14</version>
        </dependency>

        <!-- mycompany -->
        <dependency>
            <groupId>com.mycompany.batch</groupId>
            <artifactId>gz-batch-comptroller</artifactId>
            <version>1.0.0-RELEASE</version>
        </dependency>

    </dependencies>

    <build>
        <sourceDirectory>src/main/java</sourceDirectory>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-enforcer-plugin</artifactId>
                <version>3.0.0-M2</version>
                <executions>
                    <execution>
                        <id>enforce-maven</id>
                        <goals>
                            <goal>enforce</goal>
                        </goals>
                        <configuration>
                            <rules>
                                <requireMavenVersion>
                                    <version>3.6.0</version>
                                </requireMavenVersion>
                            </rules>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-javadoc-plugin</artifactId>
                <version>3.1.0</version>
                <executions>
                    <execution>
                        <id>attach-javadocs</id>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>${java-version}</source>
                    <target>${target-java-version}</target>
                    <compilerArgument>-Xlint:all</compilerArgument>
                    <showWarnings>true</showWarnings>
                    <showDeprecation>true</showDeprecation>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.1.1</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>false</addClasspath>
                            <Main-Class>com.mycompany.xmlrpc.server.Server</Main-Class>
                        </manifest>
                        <manifestEntries>
                            <Built-By>Michael Davidson</Built-By>
                            <Class-Path>.</Class-Path>
                        </manifestEntries>
                    </archive>
                </configuration>
            </plugin>
            <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">
                                    <manifestEntries>
                                        <Main-Class>com.mycompany.xmlrpc.server.Server</Main-Class>
                                        <Class-Path>.</Class-Path>
                                    </manifestEntries>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>3.1.1</version>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.mycompany.xmlrpc.server.Server</mainClass>
                        </manifest>
                        <manifestEntries>
                            <Class-Path>. gz-batch-comptroller-1.0.0-RELEASE.jar</Class-Path>
                        </manifestEntries>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M3</version>
                <configuration>
                    <testFailureIgnore>true</testFailureIgnore>
                </configuration>
            </plugin>
        </plugins>
    </build>


</project>

enter image description here

这是我所需要的,包括装满的罐子。

enter image description here

我看到了与此主题相关的其他主题,并且我认为pom是正确的。但是,我似乎无法到达那里。

谢谢。

2 个答案:

答案 0 :(得分:1)

我不确定您为什么要使用所有3个maven插件-jarshadeassembly

您可以尝试使用shade还是assembly插件。您还可以参考(如果尚未)以下链接来检查这些插件的使用情况:

Difference between maven plugins ( assembly-plugins , jar-plugins , shaded-plugins)

答案 1 :(得分:0)

请从maven-assembly-plugin文件中完全删除maven-jar-pluginpom.xml,然后按如下所示使用maven-shade-plugin

<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>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

然后运行mvn clean package并检查生成的JAR文件。