Copy File/Directory with Maven resource plugin

We can use Maven resource plugin to copy files and directory to any folder/path. Please refer the below snippet from pom.xml. This will be used to copy the config files from ${project.build.directory}/config to /opt/config folder.


 <plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.7</version>
        <executions>
          <execution>
            <id>copy-resources</id>
            <phase>package</phase>
            <goals>
              <goal>copy-resources</goal>
            </goals>
            <configuration>
              <outputDirectory>/opt/config</outputDirectory>
              <resources>
                <resource>
                  <directory>${project.build.directory}/config</directory>
                  <filtering>false</filtering>
                </resource>
              </resources>
            </configuration>
          </execution>         
        </executions>
      </plugin>

Create a Fat JAR

To create a FAT jar contains all the dependent classes and Jars, Please use the below approach

Maven:

maven-assembly

 

SBT:

Add the sbt-assembly plugin in the plugins.sbt as below


addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "0.14.1")

Change the version appropriately and run sbt assembly to create the Fat Jar

mvn install vs mvn package

mvn install:

It will compile the code and also package it and finally install the jar in the local repository so that other projects can use it

mvn package:

It will also compile the code and package it but it won’t install the packaged jar in the local repository

MAVEN – Reduce build time in local enviornment

In most of the Java projects, Maven is being used as a build tool. We use “mvn –U install” command to build the project. While building the project, the maven will download the dependent jars (mentioned in POM.xml file) from the project specific repository/maven repository. Once the build is completed, then all the downloaded jars will be stored in local machine(C:/Users/<>/.m2).

Please run .m2 command on the Run window and check it out.

This kind of build process will take more time as it needs to download all the jars. But this is not required all the times. We can use the downloaded local jars available in the local repository for building the project in the next time

To do that we need to pass –o option while building our project. For example, mvn –o install. This will speed up our code build time.