如何使用Maven下载具有所有依赖项的jar?

由于我需要下载很多jar /依赖项来为特殊应用创建特殊的JRE发行版,因此避免在maven存储库网站上进行下载。

我在Maven上创建了一个项目,将依赖项放在pom.xml上,它已经下载了所有jar文件~~ .m2 / repository(用户文件夹),但是很难通过“ .m2 /存储库”文件夹中,而我真的不知道我的项目使用了什么,因为该文件夹包含了所有项目中的所有jar。

是否有任何魔术命令仅复制项目所需的jar?

Doris9900 回答:如何使用Maven下载具有所有依赖项的jar?

是的

create a app一样,编辑pom.xml,并将所有依赖项放入依赖项块。

<dependencies>
      <dependency>      
        <groupId>org.deeplearning4j</groupId>      
        <artifactId>deeplearning4j-core</artifactId>
        <version>1.0.0-beta5</version>    
      </dependency>         
      <dependency>      
        <groupId>org.deeplearning4j</groupId>      
        <artifactId>deeplearning4j-modelimport</artifactId>      
        <version>1.0.0-beta5</version>    
      </dependency>                       
      <dependency>      
        <groupId>org.nd4j</groupId>      
        <artifactId>nd4j-native-platform</artifactId>
        <version>1.0.0-beta5</version>    
      </dependency>
      <dependency>      
        <groupId>com.google.cloud.dataflow</groupId>      
        <artifactId>google-cloud-dataflow-java-sdk-all</artifactId>  
        <version>2.5.0</version>     
      </dependency>
   </dependencies>

然后您可以创建您的程序包,打开一个终端,导航到您的项目并执行以下命令:

mvn软件包

构建后,您可以使用下一条命令仅复制项目使用的jar(以及所有依赖项):

mvn依赖项:复制依赖项

并检查文件夹“ 目标/依赖项”,所有jar文件将存储在此文件夹中,您可以将其复制到JRE / lib / ext。 target/deoendency folder

,

您可以为此使用maven-dependency-plugin。参见下面的示例pom.xml文件。

pom.xml

<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://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.learn</groupId>
    <artifactId>stack-overflow</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.googlecode.json-simple</groupId>
            <artifactId>json-simple</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.dbunit</groupId>
            <artifactId>dbunit</artifactId>
            <version>2.6.0</version>
            <scope>test</scope>
        </dependency>

    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <artifactItems>
                        <artifactItem>
                            <groupId>org.dbunit</groupId>
                            <artifactId>dbunit</artifactId>
                            <version>2.6.0</version>
                            <overWrite>false</overWrite>
                        </artifactItem>
                        <artifactItem>
                            <groupId>com.googlecode.json-simple</groupId>
                            <artifactId>json-simple</artifactId>
                            <version>1.1.1</version>
                        </artifactItem>
                    </artifactItems>
                    <outputDirectory>C:/Temp</outputDirectory>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

有关使用此插件的更多详细信息,请收听

https://maven.apache.org/plugins/maven-dependency-plugin/examples/copying-artifacts.html

编辑:

对不起,由于您要复制所有依赖项,因此可以使用以下配置。

pom.xml

<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://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.learn</groupId>
    <artifactId>stack-overflow</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.googlecode.json-simple</groupId>
            <artifactId>json-simple</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.dbunit</groupId>
            <artifactId>dbunit</artifactId>
            <version>2.6.0</version>
            <scope>test</scope>
        </dependency>

    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>C:/Temp</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                            <excludeTransitive>true</excludeTransitive><!-- Use this if you want 
                                to copy only the dependencies that you deplacred. -->
                        </configuration>
                    </execution>
                </executions>

            </plugin>
        </plugins>
    </build>
</project>
本文链接:https://www.f2er.com/3149923.html

大家都在问