【问题标题】:How to use a maven to download jars with all dependencies?如何使用 maven 下载所有依赖项的 jars?
【发布时间】:2020-03-03 06:46:27
【问题描述】:

由于我需要下载大量的 jars/依赖项来为特殊应用程序创建一个特殊的 JRE 发行版,以避免在 maven 存储库网站上下载。

我在 Maven 上创建了一个项目,将我的依赖项放在 pom.xml 上,它已经下载了所有 jars“~.m2/repository”(用户文件夹),但是很难逐个 jar 获取“.m2/ repository" 文件夹,我真的不知道我的项目使用什么,因为这个文件夹包含来自所有项目的所有 jar。

有什么神奇的命令只复制我项目需要的 jar 包吗?

【问题讨论】:

  • 请问您为什么希望避免从 maven 下载依赖项?如果是这种情况,您也可以在项目的本地文件夹中添加您包含在项目中的 jars?
  • “我的项目需要”?您是指构建项目所需的所有 jar,还是运行时需要的所有 jar?

标签: java maven jar dependencies


【解决方案1】:

是的,

正如你所做的那样,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 包

在 BUILD 之后,您可以使用下一个命令仅复制项目使用的 jars(以及所有依赖项):

mvn 依赖:复制依赖

并检查文件夹“target/dependency”,所有 jar 都将存储到此文件夹中,您可以将其复制到您的 JRE/lib/ext 中。

【讨论】:

    【解决方案2】:

    您可以为此使用 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-04
      • 2018-08-29
      • 2016-01-27
      • 2016-09-30
      • 1970-01-01
      • 2020-04-30
      • 1970-01-01
      • 2011-01-18
      相关资源
      最近更新 更多