Wednesday, 10 July 2019

Maven: copy dependencies into target/lib


In this post, I am going to show you how can you copy all the dependencies of your maven project into 'target/lib' directory using 'maven-dependency-plugin'.

Step 1: Create  new simple maven project by executing below command.
mvn archetype:generate -DarchetypeArtifactId=maven-archetype-quickstart

Once you execute the command, mvn opens interactive mode and asks groupId, artifactId, version and package details.

Provide groupId, artifactId as helloWorld, version as 1, package name as com.sample.app.

[INFO] Generating project in Interactive mode
Define value for property 'groupId': helloWorld
Define value for property 'artifactId': helloWorld
Define value for property 'version' 1.0-SNAPSHOT: : 1
Define value for property 'package' helloWorld: : com.sample.app
Confirm properties configuration:
groupId: helloWorld
artifactId: helloWorld
version: 1
package: com.sample.app
 Y: : Y
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.0
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: basedir, Value: C:\Users\krishna\Documents\Study\Maven\projects
[INFO] Parameter: package, Value: com.sample.app
[INFO] Parameter: groupId, Value: helloWorld
[INFO] Parameter: artifactId, Value: helloWorld
[INFO] Parameter: packageName, Value: com.sample.app
[INFO] Parameter: version, Value: 1
[INFO] project created from Old (1.x) Archetype in dir: C:\Users\krishna\Documents\Study\Maven\projects\helloWorld
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 06:23 min
[INFO] Finished at: 2018-05-03T12:02:06+05:30
[INFO] ------------------------------------------------------------------------


It creates project structure like below.



Step 2: Add some dependencies to 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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>helloWorld</groupId>
  <artifactId>helloWorld</artifactId>
  <packaging>jar</packaging>
  <version>1</version>
  <name>helloWorld</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
 
 <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-core</artifactId>
  <version>5.0.5.RELEASE</version>
 </dependency>

  </dependencies>
</project>


Step 3: Go the the location, where pom.xml is located and execute the command ‘mvn clean install’.

[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ helloWorld ---
[INFO] Building jar: C:\Users\krishna\Documents\Study\Maven\projects\helloWorld\target\helloWorld-1.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ helloWorld ---
[INFO] Installing C:\Users\krishna\Documents\Study\Maven\projects\helloWorld\target\helloWorld-1.jar to C:\Users\krishna\.m2\repository\helloWorld\helloWorld\1\helloWorld-1.jar
[INFO] Installing C:\Users\krishna\Documents\Study\Maven\projects\helloWorld\pom.xml to C:\Users\krishna\.m2\repository\helloWorld\helloWorld\1\helloWorld-1.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.385 s
[INFO] Finished at: 2018-05-03T13:31:36+05:30
[INFO] ------------------------------------------------------------------------

It generates a jar file under the target folder.

Copy dependencies into target/lib
There are some situations, where you want to download all the dependencies and put them in one location. You can do that by using 'maven-dependency-plugin'

Add 'maven-dependency-plugin' to the pom.xml like below.


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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>helloWorld</groupId>
  <artifactId>helloWorld</artifactId>
  <packaging>jar</packaging>
  <version>1</version>
  <name>helloWorld</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
 
 <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-core</artifactId>
  <version>5.0.5.RELEASE</version>
 </dependency>

  </dependencies>
  
   <profiles>
    <profile>
      <id>buildWithDependencies</id>
      <build>
        <plugins>
          <plugin>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
              <execution>
                <phase>install</phase>
                <goals>
                  <goal>copy-dependencies</goal>
                </goals>
                <configuration>
                  <outputDirectory>${project.build.directory}/lib</outputDirectory>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>
  
</project>


Execute the command ‘mvn clean install -PbuildWithDependencies’.

[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ helloWorld ---
[INFO] Building jar: C:\Users\I335077\Documents\Study\Maven\projects\helloWorld\target\helloWorld-1.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ helloWorld ---
[INFO] Installing C:\Users\I335077\Documents\Study\Maven\projects\helloWorld\target\helloWorld-1.jar to C:\Users\I335077\.m2\repository\helloWorld\helloWorld\1\helloWorld-1.jar
[INFO] Installing C:\Users\I335077\Documents\Study\Maven\projects\helloWorld\pom.xml to C:\Users\I335077\.m2\repository\helloWorld\helloWorld\1\helloWorld-1.pom
[INFO]
[INFO] --- maven-dependency-plugin:2.8:copy-dependencies (default) @ helloWorld ---
[INFO] Copying junit-3.8.1.jar to C:\Users\I335077\Documents\Study\Maven\projects\helloWorld\target\lib\junit-3.8.1.jar
[INFO] Copying spring-core-5.0.5.RELEASE.jar to C:\Users\I335077\Documents\Study\Maven\projects\helloWorld\target\lib\spring-core-5.0.5.RELEASE.jar
[INFO] Copying spring-jcl-5.0.5.RELEASE.jar to C:\Users\I335077\Documents\Study\Maven\projects\helloWorld\target\lib\spring-jcl-5.0.5.RELEASE.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.142 s
[INFO] Finished at: 2018-05-03T13:35:58+05:30
[INFO] ------------------------------------------------------------------------


You can see there is a ‘lib’ directory is created under ‘target’ directory. All the dependent jars are copied to the lib directory.


Exclude test dependencies
As you see above image, junit is also copied to the lib directory. But I do not want test dependencies.

You can exclude the test scoped dependencies, by adding the element <includeScope>compile</includeScope> under configuration element.

<configuration>
         <outputDirectory>${project.build.directory}/lib</outputDirectory>
         <includeScope>compile</includeScope>
</configuration>

Updated pom.xml looks like below.

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>helloWorld</groupId>
  <artifactId>helloWorld</artifactId>
  <packaging>jar</packaging>
  <version>1</version>
  <name>helloWorld</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
 
 <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
 <dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-core</artifactId>
  <version>5.0.5.RELEASE</version>
 </dependency>

  </dependencies>
  
   <profiles>
    <profile>
      <id>buildWithDependencies</id>
      <build>
        <plugins>
          <plugin>
            <artifactId>maven-dependency-plugin</artifactId>
            <executions>
              <execution>
                <phase>install</phase>
                <goals>
                  <goal>copy-dependencies</goal>
                </goals>
                <configuration>
                  <outputDirectory>${project.build.directory}/lib</outputDirectory>
      <includeScope>compile</includeScope>
                </configuration>
              </execution>
            </executions>
          </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>
  
</project>

Execute the command 'mvn clean install -PbuildWithDependencies'.


After executing the command, the test scoped dependencies are not copied to lib folder.

Previous                                                    Next                                                    Home

No comments:

Post a Comment