Why "mvn deploy" command is rebuilding .jar? - maven

I am working on the spring-boot java application. I am trying to build and publish .jar using maven with profile.but somehow mvn deploy command rebuilds .jar again.
option 1:I used mvn clear install -Pdev and did mvn deploy -Dmaven.install.skip=truewithout profile and its deploying default .jar file
option 2: I passed profile id during publish too.mvn deploy -Dmaven.install.skip=true ITs working fine but its rebuilding everything again and we do not want to use maven profile name again during mvn deploy
pom.xml
<project...>
...
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>dev</id>
<properties>
<spring.profile.id>dev</spring.profile.id>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</profile>
<profile>
<id>prod</id>
<properties>
<spring.profile.id>prod</spring.profile.id>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<version>2.1.6.RELEASE</version>
</dependency>
</dependencies>
</profile>
</profiles>
</project>
mvn clean install -Pdev
[INFO] --- maven-jar-plugin:3.1.2:jar (default-jar) # xyz-
profile
[INFO] Building jar: /sys_apps_01/jenkins/workspace/xyz-profile-0.0.3-
SNAPSHOT.jar
mvn deploy mvn deploy -Pdev
I am getting below logs for both deploy command:
[DEBUG] isUp2date: false (Destination /sys_apps_01/jenkins/workspace/xyz-
profile-0.0.3-SNAPSHOT.jar not found.)
[INFO] Building jar: /sys_apps_01/jenkins/workspace/xyz-profile-0.0.3-
SNAPSHOT.jar
Can anyone help me to understand,why its rebuilding again while deploy ?

You should read about Maven lifecycle for understand what is happening : https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html
So when you're doing a deploy command maven trigger all previous goal in the lifecycle :
validate
compile
test
package
verify
install
Then deploy

Related

Are maven's profile-specific dependencies propagated to the published artifact?

I need to publish an artifact twice - each with a different set of dependencies. I'm happy to give the artifacts different ids but would like to use the same pom.xml file. I've attempted this with profile-specific dependencies, but I can't seem to get these dependencies visible as transitive dependencies in the artifacts. Is that possible?
pom1.xml - creates profile-specific builds. The A profile adds a dependency on commons-collections 3.2.2 and profile B depends on 3.2.1.
pom2.xml - simply defines a dependency on pom1's artifact.
src/main/java/tt.java - defines an empty class to provide something to compile by pom1.
mvn version is 3.8.5.
% mvn -f pom1.xml -P A dependency:tree
...
[INFO] org.acme:testing:jar:testing
[INFO] \- commons-collections:commons-collections:jar:3.2.2:compile
...
% mvn -f pom1.xml -P B dependency:tree
...
[INFO] org.acme:testing:jar:testing
[INFO] \- commons-collections:commons-collections:jar:3.2.1:compile
...
% mvn -f pom1.xml -P A clean install
...
% mvn -f pom2.xml dependency:tree
...
[INFO] org.acme:testing2:jar:testing
[INFO] \- org.acme:testing:jar:testing:compile
[INFO] \- commons-collections:commons-collections:jar:3.2.1:compile
It seems to me pom2's dependencies should have been on 3.2.2 from profile A.
Any idea on how I might make the dependencies from profile A transitive to dependent poms?
pom1.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.acme</groupId>
<artifactId>testing</artifactId>
<version>testing</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>A</id>
<properties>
<profile.name>cuda11.2</profile.name>
</properties>
<dependencies>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.2</version>
</dependency>
</dependencies>
</profile>
<profile>
<id>B</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<profile.name>cpu</profile.name>
</properties>
<dependencies>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
</dependencies>
</profile>
</profiles>
<!-- If I move this dependency out of the profile, then it becomes visible
as a transitive depenency to other projects referencing this pom's artifact.
<dependencies>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.2</version>
</dependency>
</dependencies>
-->
</project>
pom2.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.acme</groupId>
<artifactId>testing2</artifactId>
<version>testing</version>
<dependencies>
<dependency>
<groupId>org.acme</groupId>
<artifactId>testing</artifactId>
<version>testing</version>
</dependency>
</dependencies>
</project>

Spring Boot Not Resolving Properties Variable When Using spring-boot:run

This was working for me and I'm not sure what changed.. I have my spring boot profile configured to be set based on a maven profile. The basics:
application.properties:
spring.profiles.active=#environment#
pom.xml:
<profiles>
<profile>
<id>development</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<environment>development</environment>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</profile>
<profile>
<id>production</id>
<properties>
<environment>production</environment>
</properties>
</profile>
</profiles>
When I run mvn clean package -Pdevelopment I see the line The following profiles are active: development.
Yet when I run mvn spring-boot:run -Pdevelopment I see the line The following profiles are active: #environment#.
Using the spring-boot:run command seems to not be able to resolve application property variables based on maven environment variables. Anyone know why? I tried adjusting the spring starter version without success.
According to the docs, you could tune the profiles to enable when running the application as follows:
$ mvn spring-boot:run -Dspring-boot.run.profiles=development
If not, try to comment your "spring.profiles.active" property in application.properties, that should work!
See also this thread.
Based on this tip in the docs, I added a configuration to my spring-boot-maven-plugin which broke this functionality:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<addResources>true</addResources>
</configuration>
</plugin>
Removing the addResources configuration restores the property expansion behavior.

How do you configure a Maven/SpringBoot Project's pom.xml for docker?

I am attempting to dockerize my SpringMVC application via Maven. My intent is to have an image that I can then proceed to expose and display via my web browser.
Unfortunately, in following this guide, I appear to still lack a critical piece of understanding concerning the pom.xml edits I must make to achieve this, and the Dockerfile.
======
Here is the Dockerfile:
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG JAR_FILE
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
======
Here is the source code's pom.xml in its latest revision.
======
Here is my latest attempt at revision, in following the example pom.xml of the SpringIO guide I referenced above (dependencies section not included).
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.davidonus</groupId>
<artifactId>davidonusSpringDemo1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- tag::packaging[] -->
<packaging>jar</packaging>
<name>davidonusSpringDemo1</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<!-- tag::docker[] -->
<docker.image.prefix>springio</docker.image.prefix>
</properties>
<profiles>
<profile>
<id>DavidSpringTime</id>
</profile>
</profiles>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- tag::plugin[] -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.4.9</version>
<configuration>
<repository>${docker.image.prefix}/${project.artifactId}</repository>
</configuration>
</plugin>
<!-- end::plugin[] -->
<!-- tag::unpack[] -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>unpack</id>
<phase>package</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<!-- end::unpack[] -->
</plugins>
</build>
Here are my present results, using the command mvn install build:docker
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 12.050 s
[INFO] Finished at: 2019-06-15T13:24:01-04:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-dependency-plugin:3.1.1:unpack (unpack) on project davidonusSpringDemo1: Unable to update Marker timestamp: /home/david/Desktop/DevOps2019/springBoot/teluskoSpringBoot/target/dependency-maven-plugin-markers/com.davidonus-davidonusSpringDemo1-jar-0.0.1-SNAPSHOT.marker: Unable to update last modified timestamp on marker file /home/david/Desktop/DevOps2019/springBoot/teluskoSpringBoot/target/dependency-maven-plugin-markers/com.davidonus-davidonusSpringDemo1-jar-0.0.1-SNAPSHOT.marker -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
======
In summary, given my original pom.xml, what changes would you implement to make my SpringBoot + Maven project deployable as a docker image and container?
Furthermore, are there adaptions to my Dockerfile that you'd make? Your consultation is appreciated. Thank you.
If you have your springboot code ready with pom.xml. Then follow below steps to containerize your application.
git clone https://github.com/dnmorris7/teluskoSpringBoot (I'm cloning your springboot code)
git checkout module5 (checked out module5 branch)
Created Dockerfile in your git codebase with following contents:
FROM maven:3.6-jdk-8-slim AS build
COPY . /usr/src/app/
WORKDIR /usr/src/app/
RUN mvn -f /usr/src/app/pom.xml clean package
FROM java:8-alpine
WORKDIR /
COPY --from=build /usr/src/app/target/*.jar /app.jar
CMD java -jar app.jar
NOTE: I'm using docker multi-stage build where in first stsage maven builds the jar and in the second stage we copy that jar in java image.
Now build your docker image docker build -t appimage:v1 .
Run your docker container docker run -it -d -p 9090:9090 appimage:v1
Hit the api to check if its working fine.
$ curl localhost:9090/home
{"timestamp":"2019-06-16T05:34:26.655+0000","status":404,"error":"Not Found","message":"/pages/home.jsp","path":"/home"}
Please hit the correct base url, I tried with /home
NOTE: If you want to provide your own custom application.properties then change the java -jar command in Dockerfile to CMD java -jar app.jar --spring.config.additional-location=application.properties and change the docker run command to docker run -it -d -v application.properties:/application.properties -p 9090:9090 appimage:v1 where application.properties is the one which you provide from outside.
I think not much is missing. Or even better there may be even too many things.
First, you need to tell the docker maven plugin to run. The maven lifecycle defines which plugins run at what phase. So all other plugins need an execution configuration somewhere (in a parent pom or in yours). Second, there is no need to unpack the created jar file. Spring Boot will create an executable jar file automatically. You only need to tell the docker maven plugin about it (where it is created)
This would be the Dockerfile:
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG JAR_FILE
ADD target/${JAR_FILE} /usr/share/myapp.jar
ENTRYPOINT ["java","-jar","/usr/share/myapp.jar"]
And this your pom:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework</groupId>
<artifactId>gs-spring-boot-docker</artifactId>
<version>0.1.0</version>
<packaging>jar</packaging>
<name>Spring Boot Docker</name>
<description>Getting started with Spring Boot and Docker</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath />
</parent>
<properties>
<docker.image.prefix>springio</docker.image.prefix>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- tag::plugin[] -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.4.9</version>
<executions>
<execution>
<id>default</id>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
<configuration>
<repository>${docker.image.prefix}/${project.artifactId}</repository>
<buildArgs>
<JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
</buildArgs>
</configuration>
</plugin>
<!-- end::plugin[] -->
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
The unpack goal of the dependency plugin can be removed. This way the jar is added into the image and run directly. Hope it works!

Maven scope test needs to resolve depdencies

I have a dependency in my pom with <scope>test</scope>. As I understood the scope concept of maven the dependency should only be required during test builds. Nevertheless maven trys to download the dependency during a mvn package which is why I get following build failure:
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.278 s
[INFO] Finished at: 2016-04-19T22:11:59+02:00
[INFO] Final Memory: 5M/15M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal on project my-module: Could not resolve dependencies for project com.mycompany.app:my-module:jar:1: Failure to find group-a:artifact-b:jar:tests:1.0 in https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval of central has elapsed or updates are forced -> [Help 1]0
I use following pom:
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-module</artifactId>
<version>1</version>
<dependencies>
<dependency>
<groupId>group-a</groupId>
<artifactId>artifact-b</artifactId>
<version>1.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Is this intended behavior of maven?
Are there any mitigations to ignore the dependency during package/install/deploy builds?
Any help is appreciated
I couldn't solve the issue, but I found a comfortable way to mitigate the problem. Durring my package I use -Dmaven.test.skip=true parameter. So I defined two profiles one with the dependency and one without. When tests are skipped I deactivate the profile with the dependency.
Nevertheless I would appreciate a solution with the scope tag
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany.app</groupId>
<artifactId>my-module</artifactId>
<version>1</version>
<profiles>
<profile>
<id>default</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<dependencies>
<dependency>
<groupId>group-a</groupId>
<artifactId>artifact-b</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
</profile>
<profile>
<id>skip-tests</id>
<activation>
<property>
<name>maven.test.skip</name>
<value>true</value>
</property>
</activation>
</profile>
</profiles>
</project>

MAVEN : Run Multiple Maven Project using Maven Test

I have 3 maven projects. Project1, Project2 & Project3.
Project3 depends on Project1 & Project2 and Project2 depends on Project1.
For this I have added this in Project2 pom.xml file
<modelVersion>4.0.0</modelVersion>
<groupId>Project2</groupId>
<artifactId>Project2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>Project1</groupId>
<artifactId>Project1</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
My pom.xml for Project3 is -
<modelVersion>4.0.0</modelVersion>
<groupId>Project3</groupId>
<artifactId>Project3</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>Project1</groupId>
<artifactId>Project1</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>Project2</groupId>
<artifactId>Project2</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
In the project 3 I have added testng.xml file to run the test. Now If I run this testng.xml file then all my test cases are running successfully. By If I tried to run test cases using maven test then its failing.
I have included testng.xml file in the pom file below dependencies to run testng test using maven -
<build>
<!-- Source directory configuration -->
<sourceDirectory>src</sourceDirectory>
<plugins>
<!-- Following plugin executes the testng tests -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.14.1</version>
<configuration>
<!-- Suite testng xml file to consider for test execution -->
<suiteXmlFiles>
<suiteXmlFile>Tng1.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
<!-- Compiler plugin configures the java version to be used for compiling the code -->
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
It is showing error message -
The POM for Project1:Project1:jar:0.0.1-SNAPSHOT is missing, no dependency information available.
The POM for Project2:Project2:jar:0.0.1-SNAPSHOT is missing, no dependency information available
Failed to execute goal on project Project3: Could not resolve dependencies for project Project3:Project3:jar:0.0.1-SNAPSHOT: The following artifacts could not be resolved: Project1:Project1:jar:0.0.1-SNAPSHOT, Project2:Project2:jar:0.0.1-SNAPSHOT: Could not find artifact Project1:Project1:jar:0.0.1-SNAPSHOT -> [Help 1
Please Help !!!
You have to install Project1 and Project2 in your maven repository first.
To do that launch the command
mvn clean install
in Project1 and Project2 directory (start with Project1 because Project2 has a dependecy to Project1)
EDIT:
For Jenking, I usually create a Root pom with module declaration. Maven will order you module by dependencies.
In your root pom you will have
<modules>
<module>project1</module>
<module>project2</module>
<module>project3</module>
</modules>
And in your submodule
<parent>
<groupId>com.myGroupId</groupId>
<artifactId>project-root</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
Yon can check http://books.sonatype.com/mvnex-book/reference/multimodule.html for full example

Resources