why mvn doesn't generate fat jar - maven

The following is my pom.xml, I want to build a fat Jar with command "mvn clean pacckage", so I have added a plugin, but unfortunatedly it doesn't generate a far jar, I only see a management-service-1.0-SNAPSHOT.jar, it doesn't contains any dependency jars.
<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>com.xx.yy.zz</groupId>
<artifactId>management-service</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>management-service</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<properties>
<maven.compiler.parameters>true</maven.compiler.parameters>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.1</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

our execution needs to go in , not See maven.apache.org/plugins/maven-assembly-plugin/usage.html

Related

how to associate a goal to a specific phase in maven

I know maven's deploy meaning is "send the artifact to the repository" but what I want to do is associate the tomcat7-maven-plugin's deploy goal to the deploy phase or another one like install. I'm not able to associate it with the following configuration:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>userpom</artifactId>
<groupId>com.test</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>userweb</artifactId>
<packaging>war</packaging>
<name>userweb</name>
<url>http://www.test.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.test</groupId>
<artifactId>userservices</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.geronimo.specs</groupId>
<artifactId>geronimo-servlet_3.0_spec</artifactId>
<version>1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
</dependencies>
<build>
<finalName>userweb</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<!--<plugin>-->
<!--<artifactId>maven-deploy-plugin</artifactId>-->
<!--<version>2.8.2</version>-->
<!--</plugin>-->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<url>http://localhost:8080/manager/text</url>
<server>TomcatServer</server>
<path>/usertest</path>
<update>true</update>
</configuration>
<executions>
<execution>
<id>tomcat-deploy</id>
<phase>install</phase>
<goals>
<goal>deploy</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
while the parent pom is the following:
<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>com.test</groupId>
<artifactId>userpom</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>userservices</module>
<module>userweb</module>
</modules>
<packaging>pom</packaging>
<name>userpom</name>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.5.RELEASE</version>
</dependency>
</dependencies>
</project>
basically, it's not executed and I have to call it manually:
mvn tomcat7:deploy
within the userweb folder. Any hint?
You're actually defining your tomcat7-maven-plugin only in pluginManagement but do not refer it anywhere outside in the build.plugins section.
A slightly altered version of your pom.xml showing only the relevant parts could be:
<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">
<parent>
<artifactId>userpom</artifactId>
<groupId>com.test</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
[…]
<build>
<finalName>userweb</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<!--<plugin>-->
<!--<artifactId>maven-deploy-plugin</artifactId>-->
<!--<version>2.8.2</version>-->
<!--</plugin>-->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<url>http://localhost:8080/manager/text</url>
<server>TomcatServer</server>
<path>/usertest</path>
<update>true</update>
</configuration>
<executions>
<execution>
<id>tomcat-deploy</id>
<phase>install</phase>
<goals>
<goal>deploy</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
If all artifacts which inherit the parent pom.xml shall benefit from your pre-configured tomcat7-maven-plugin then pull the pluginManagement section out of your sub pom.xml and put it into the build section of your parent pom.xml.

How to Build specific module in a multi module spring-boot maven project

I have create a multi module spring boot maven project.
But when I use
mvn clean package -pl module2 spring-boot:run
in console. It tell me that some class in module1 can not find.
But I have added the dependency in module2. The module2 is the final project.
the project structure is as follows.
parent project`s pom.xml
<groupId>com.example</groupId>
<artifactId>multi-module</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<modules>
<module>module1</module>
<module>module2</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/>
</parent>
<properties>
...
</properties>
</dependencies>
...
<dependencyManagement>
...
</dependencyManagement>
the pom in module1:
<groupId>com.example</groupId>
<artifactId>module1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>com.example</groupId>
<artifactId>multi-module</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
the pom.xml in module2:
<artifactId>personalinfo</artifactId>
<name>personalinfo</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<parent>
<groupId>com.example</groupId>
<artifactId>multi-module</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>com.example</groupId>
<artifactId>module1</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
You need to tweak your pom.
Parent pom:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Module pom (containing your Main)
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>Your mainClass</mainClass>
<fork>true</fork>
<skip>false</skip>
</configuration>
</plugin>
</plugins>
</build>
Pay emphasis on Configuration > skip
The answer from #bitscanbyte is correct, but the configuration can be simplified:
Parent pom
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
Module pom (the module in which the #SpringBootApplication is located)
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<skip>false</skip>
</configuration>
</plugin>
</plugins>
</build>
A simple demo project can be found here: https://github.com/drahkrub/spring-boot-multi-module

how to access variable in pom.xml from external properties file?

I need to get value through a key in pom.xml from external properties files. Location of properties file is in src/main/resources/dev.properties.
I have tried to solve this by using properties in maven. Please help me.
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>Simple_project</groupId>
<artifactId>Project</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Project Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
</dependencies>
<build>
<finalName>${Project.Name}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<outputDirectory>${path}</outputDirectory>
</configuration>
</plugin>
</plugins>
</build>
</project>
// properties File
dev.properties:
projectPath=/home/user/warfile
Project.Name = newProject
Thanks & Regards,
Tharanya B
You have to use the properties plugin:
Plugin
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0.0</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>${basedir}/${propertiesFile}</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
Mvn Command
mvn -DpropertiesFile=dev.properties properties:read-project-properties clean install
Alternatively
You can hardcode the file:
<file>${basedir}/dev.properties</file>

Deploying multiple WARS into Tomcat Using Maven

I have two applications and want to deploy these two WARS into tomcat 7.0 using maven. I tried to see all posts of Stackoverflow regarding this and modeled my POM accordingly. However, while running the parent POM, I noticed that only the first application(RestApp) is getting deployed not the second app. Could you please see if there is some correction required in POM
Parent POM
<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>my.parent</groupId>
<artifactId>ParentProj</artifactId>
<packaging>pom</packaging>
<version>1.0</version>
<name>Parent Maven Webapp</name>
<!-- <url>http://maven.apache.org</url> -->
<properties>
<!--Test -->
<project.build.finalName>ParentProj</project.build.finalName>
</properties>
<modules>
<module>RESTApp</module>
<module>WebIntegrationApp</module>
</modules>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>${project.build.finalName}</finalName>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<version>2.5</version>
</plugin>
<plugin>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-maven2-plugin</artifactId>
<version>1.1.3</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.16</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.16</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<executions>
<execution>
<id>deploy</id>
<phase>pre-integration-test</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
<configuration>
<warSourceDirectory>WebContent</warSourceDirectory>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Here is the child Apps
RestApp
<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>
<parent>
<groupId>my.parent</groupId>
<artifactId>ParentProj</artifactId>
<version>1.0</version>
<relativePath>../</relativePath>
</parent>
<artifactId>RestApp</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<properties>
<!--Test -->
<project.build.finalName>RestApp</project.build.finalName>
</properties>
<dependencies>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.25</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
</plugin>
</plugins>
</build>
WebIntegrationApp
<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>
<parent>
<groupId>my.parent</groupId>
<artifactId>ParentProj</artifactId>
<version>1.0</version>
<relativePath>../</relativePath>
</parent>
<artifactId>WebIntegrationApp</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<properties>
<!--Test -->
<project.build.finalName>WebIntegrationApp</project.build.finalName>
</properties>
<dependencies>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.25</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webappDirectory>${project.build.directory}/tmp</webappDirectory>
</configuration>
</plugin>
</plugins>
</build>

War file deployment using maven and eclipse in JBOSS

I am trying to deploy war file in jboss-as-7.1.1.final. It is giving message BUILD SUCCESS but there is no war file in deployment folder. i searched on google and found many solution and tried almost all of them but it is not working.
my pom.xml is
<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>com.test.demo</groupId>
<artifactId>TEST</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>TEST Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>TEST</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>${JAVA.VERSION}</source>
<target>${JAVA.VERSION}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
<configuration>
<filteringDeploymentDescriptors>true</filteringDeploymentDescriptors>
<warName>TEST.war</warName>
</configuration>
</plugin>
<plugin>
<groupId>org.jboss.as.plugins</groupId>
<artifactId>jboss-as-maven-plugin</artifactId>
<version>7.1.1.Final</version>
<configuration>
<force>true</force>
<jbossHome>F:\\TEST\\LUNA WITH JBOSS7\\jboss-as-7.1.1.Final\\standalone\\deployments</jbossHome>
<hostname>172.16.3.95</hostname>
<fileNames>
<fileName>target/TEST.war</fileName>
</fileNames>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<JAVA.VERSION>1.7</JAVA.VERSION>
<NAME>TEST</NAME>
</properties>
</project>
Please tell me what is wrong?

Resources