Maven POM file - Yash-777/LearnJava GitHub Wiki
Available lifecycle phases
|goals
are:
validate, initialize, generate-sources, process-sources, generate-resources, process-resources, compile, process-classes, generate-test-sources, process-test-sources, generate-test-resources, process-test-resources, test-compile, process-test-classes, test, prepare-package, package, pre-integration-test, integration-test, post-integration-test, verify, install, deploy, pre-clean, clean, post-clean, pre-site, site, post-site, site-deploy
To run any other goals like shade:shade
of shade plugin along with package goal, we run as mvn package shade:shade
.
By using executions we can add shade goal
to run along with package goal
as shown below. mvn package
.
Creating a Shaded JAR « The goals for the Shade Plugin are bound to the package phase in the build lifecycle.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
How to create a jar containing test classes
<executions>
<execution>
<goals> <!-- ScreenRecorder-0.0.1-SNAPSHOT.jar, ScreenRecorder-0.0.1-SNAPSHOT-tests.jar -->
<goal>test-jar</goal>
</goals>
</execution>
</executions>
if we have a resource on some location « src/main/resources/config.properties | target/my-resources/localDev.jar
and
if we want to place them in class path « target/classes/config.properties | target/classes/localDev.jar
So, that they will be packaged in to jar/war/zip file.
<build>
<resources>
<resource>
<targetPath>./libClasspath</targetPath>
<filtering>false</filtering>
<directory>${project.build.directory}/my-resources</directory>
<includes>
<include>**/*.txt</include>
</includes>
<excludes>
<exclude>**/*test*.*</exclude>
</excludes>
</resource>
</resources>
</build>
Download maven dependencies and save into specified folder my-resources
. Executable jar with dependencies
we specify the goal copy-dependencies, which tells Maven to copy these dependencies into the specified outputDirectory.
<build>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/my-resources</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
</build>
Executable JAR with Maven baeldung.com
- using
maven-jar-plugin
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<archive>
<manifest>
<mainClass>io.github.yash777.xuggler.VideoFrame</mainClass>
<!--
<arguments>
<argument>arg0</argument>
<argument>arg1</argument>
</arguments>
-->
<addClasspath>true</addClasspath>
<classpathPrefix>libClasspath</classpathPrefix>
</manifest>
<manifestEntries>
<Class-Path>.</Class-Path>
</manifestEntries>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<!-- <finalName>webappJar</finalName> -->
<charset>utf-8</charset>
</configuration>
</plugin>
META-INF « MANIFEST.MF
classpath
Manifest-Version: 1.0
Built-By: yashwanth.m
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_144
Main-Class: io.github.yash777.xuggler.VideoFrame
Class-Path: libClasspath/xuggle-xuggler-5.4.jar libClasspath/slf4j-api-1.7.2.jar libClasspath/slf4j-simple-1.7.2.jar
- using shade plugin
&
Selecting Contents dependent jar file.class
file for Uber JARincludes-excludes
Use the maven-shade-plugin to pull in dependencies to make the standalone.jar
. shade:shade
Artifacts are denoted by a composite identifier of the form
groupId:artifactId[[:type]:classifier]
.In order to generate shaded jar like
ScreenRecorder-0.0.1-SNAPSHOT-shaded.jar
we use:<shadedArtifactAttached>true</shadedArtifactAttached>
«To create source code jar for the available lsit of shade artifacts like
ScreenRecorder-0.0.1-SNAPSHOT-sources.jar
we use<createSourcesJar>true</createSourcesJar>
.The plugin can also be configured to automatically remove all classes of dependencies that are not used by the project, thereby minimizing the resulting uber JAR:
<configuration> <minimizeJar>true</minimizeJar> ... </configuration>
Note do not use this because some classes are used at run-time:NoClassDefFoundError: com/xuggle/ferry/JNIThreadProxy
<properties>
<!-- Download the jar file classes and place into runnable jar. -->
<maven-shade-plugin-version>3.1.1</maven-shade-plugin-version>
<xuggle-groupId>xuggle</xuggle-groupId>
<slf4j-groupId>org.slf4j</slf4j-groupId>
</properties>
<build>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>${maven-shade-plugin-version}</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<createSourcesJar>false</createSourcesJar>
<minimizeJar>true</minimizeJar>
<artifactSet>
<includes>
<include>${xuggle-groupId}:*</include>
<include>${slf4j-groupId}:*</include>
</includes>
</artifactSet>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>io.github.yash777.xuggler.VideoFrame</mainClass>
</transformer>
<!-- <relocations>
<relocation>
<pattern>org.eclipse.jetty</pattern>
<shadedPattern>org.seleniumhq.jetty7</shadedPattern>
</relocation>
</relocations> -->
</configuration>
</execution>
</executions>
</plugin>
</build>
Setting Manifest Entries with the ManifestResourceTransformer
The ManifestResourceTransformer allows existing entries in the MANIFEST to be replaced and new entries added.
For example, the following sample sets
- the
Main-Class
entry to the value of the app.main.class property, - the
X-Compile-Source-JDK
entry to the value of the maven.compile.source property and - the
X-Compile-Target-JDK
entry to the value of the maven.compile.target property.
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>${app.main.class}</Main-Class>
<X-Compile-Source-JDK>${maven.compile.source}</X-Compile-Source-JDK>
<X-Compile-Target-JDK>${maven.compile.target}</X-Compile-Target-JDK>
</manifestEntries>
</transformer>
</transformers>
Introduction to the Dependency Mechanism
- System Dependencies
<dependency>
<groupId>javax.sql</groupId>
<artifactId>jdbc-stdext</artifactId>
<version>2.0</version>
<scope>system</scope>
<systemPath>${java.home}/lib/rt.jar</systemPath>
<!-- <systemPath>E://SeleniumWebDrivers.jar</systemPath>
<systemPath>${project.basedir}/lib/iamajar.jar</systemPath> -->
</dependency>
-
Guide to installing 3rd party JARs
Although rarely, but sometimes you will have 3rd party JARs that you need to put in your local repository for use in your builds, since they don't exist in any public repository like Maven Central.
mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> \
-DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>
https://stackoverflow.com/questions/4955635/how-to-add-local-jar-files-to-a-maven-project
mvn install:install-file
-Dfile=awesomeapp-1.0.1.jar \
-DpomFile=awesomeapp-1.0.1.pom \
-Dsources=awesomeapp-1.0.1-sources.jar \
-Djavadoc=awesomeapp-1.0.1-javadoc.jar \
-DgroupId=com.example \
-DartifactId=awesomeapp \
-Dversion=1.0.1 \
-Dpackaging=jar
Important note: This is marked deprecated.
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.0</version>
<executions>
<execution>
<id>tomcat-run</id>
<goals>
<goal>exec-war-only</goal>
</goals>
<phase>package</phase>
<configuration>
<path>/</path>
<enableNaming>false</enableNaming>
<finalName>webappJar</finalName>
<charset>utf-8</charset>
</configuration>
</execution>
</executions>
</plugin>
To add resources files to Build.
<properties>
<common.config.profile.directory>profileConfig/common</common.config.profile.directory>
<config.profile.directory>profileConfig/dev</config.profile.directory>
</properties>
<build>
<resources>
<resource>
<directory>${config.profile.directory}</directory>
</resource>
<resource>
<directory>${basedir}/src/main/resources</directory>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
</resource>
</resources>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<archiveClasses>true</archiveClasses>
<classifier>${war.projectname.suffix}</classifier>
<webResources>
<resource> <!-- Common resources for all environments (OR) directly we can place in
resources folder -->
<directory>${common.config.profile.directory}</directory>
<targetPath>WEB-INF/classes</targetPath>
</resource>
<resource>
<directory>${config.profile.directory}</directory>
<targetPath>WEB-INF/classes</targetPath>
<includes>
<include>*.properties</include> <!-- **/**/*.properties -->
<include>*.xml</include> <!-- context.xml : we can skip this -->
</includes>
</resource>
<resource>
<directory>${config.profile.directory}</directory>
<targetPath>META-INF</targetPath>
<includes>
<include>ReadMe.txt</include>
<include>context.xml</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
</build>