Fabio Reche
Fabio Reche

Reputation: 71

How to use a maven to download jars with all dependencies?

Since I need to download a lot of jars/dependencies to create a special JRE distribution for special application, to avoid to download on maven repository web site.

I created an project on Maven, put my dependencies on pom.xml and it has been downloaded all jars "~.m2/repository" (user folder) but it's very difficult to get jar by jar of the ".m2/repository" folder, and I really dont know what my project uses since this folder contains all jars from all projects.

Has any magic command copy only the jars of my project needs?

Upvotes: 1

Views: 3669

Answers (2)

Ramu
Ramu

Reputation: 681

You can use maven-dependency-plugin for this. See below sample pom.xml file.

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>

For more details on using this plugin, hear over to

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

Edit:

Sorry, since you want to copy all the dependencies, you can use below configuration.

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>

Upvotes: 0

Rafael Gimenes
Rafael Gimenes

Reputation: 186

Yes,

As you did, create a app, edit you pom.xml, and put all your dependencies into dependencies block.

<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>

Then ou can create your package, open a terminal navigate to your project and execute the command:

mvn package

After the BUILD you can copy only the jars that the project uses (also all dependencies), using the next command:

mvn dependency:copy-dependencies

And check the folder "target/dependency" and all jars will be stored into this folder, and you can copy this for your JRE/lib/ext. target/deoendency folder

Upvotes: 4

Related Questions