#
Integrate Jenkins with Maven (without Maven Integration plugin)
This tutorial explains to you how to integrate Jenkins with Maven (without Maven Integration plugin).
For doing this we will create the executable jar file for a Spring Boot Maven application in Jenkins.
We need to create a simple Jenkins job first.
Info
For this example we assume that :
- Source Code Management = None
- There are no build triggers
- We check "Delete workspace before build starts"
What is important, is to set a build step for running Maven. For doing this we need to choose "Invoke top-level Maven targets":
We need also to define the Maven goals and the pom.xml file location:
Click on "Save" button in order to persist the change.
Now we can run the job to see if the jar file is created. We can run the job 2 times. The first time, we can see that Maven will download the needed jar. On a second run, Maven will not download the dependencies again and the job will complete faster.
The second run will have an output like this:
Started by user admin
Running as SYSTEM
[EnvInject] - Loading node environment variables.
Building in workspace C:\ProgramData\Jenkins\.jenkins\workspace\Job1
[WS-CLEANUP] Deleting project workspace...
[WS-CLEANUP] Deferred wipeout is used...
[WS-CLEANUP] Done
[EnvInject] - Executing scripts and injecting environment variables after the SCM step.
[EnvInject] - Injecting as environment variables the properties content
JAVA_HOME="C:\MySOFT\Java\jdk-19.0.1"
[EnvInject] - Variables injected successfully.
[Job1] $ cmd.exe /C "mvn -f D:\GitHub-Projects\Projects\Spring-Boot\APIs\SimpleAPI\rest-demo\pom.xml clean package && exit %%ERRORLEVEL%%"
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for com.example:rest-demo:jar:v1
[WARNING] The expression ${parent.version} is deprecated. Please use ${project.parent.version} instead.
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[INFO]
[INFO] -----------------------< com.example:rest-demo >------------------------
[INFO] Building rest-demo v1
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:3.2.0:clean (default-clean) @ rest-demo ---
[INFO] Deleting D:\GitHub-Projects\Projects\Spring-Boot\APIs\SimpleAPI\rest-demo\target
[INFO]
[INFO] --- maven-resources-plugin:3.3.0:resources (default-resources) @ rest-demo ---
[INFO] Copying 1 resource
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.10.1:compile (default-compile) @ rest-demo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 4 source files to D:\GitHub-Projects\Projects\Spring-Boot\APIs\SimpleAPI\rest-demo\target\classes
[INFO]
[INFO] --- maven-resources-plugin:3.3.0:testResources (default-testResources) @ rest-demo ---
[INFO] skip non existing resourceDirectory D:\GitHub-Projects\Projects\Spring-Boot\APIs\SimpleAPI\rest-demo\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.10.1:testCompile (default-testCompile) @ rest-demo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to D:\GitHub-Projects\Projects\Spring-Boot\APIs\SimpleAPI\rest-demo\target\test-classes
[INFO]
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ rest-demo ---
[INFO]
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.example.restdemo.RestDemoApplicationTests
19:55:23.568 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Neither @ContextConfiguration nor @ContextHierarchy found for test class [RestDemoApplicationTests]: using SpringBootContextLoader
19:55:23.572 [main] DEBUG org.springframework.test.context.support.AbstractContextLoader - Could not detect default resource locations for test class [com.example.restdemo.RestDemoApplicationTests]: no resource found for suffixes {-context.xml, Context.groovy}.
19:55:23.572 [main] INFO org.springframework.test.context.support.AnnotationConfigContextLoaderUtils - Could not detect default configuration classes for test class [com.example.restdemo.RestDemoApplicationTests]: RestDemoApplicationTests does not declare any static, non-private, non-final, nested classes annotated with @Configuration.
19:55:23.591 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Using ContextCustomizers for test class [RestDemoApplicationTests]: [ExcludeFilterContextCustomizer, DuplicateJsonObjectContextCustomizer, MockitoContextCustomizer, TestRestTemplateContextCustomizer, DisableObservabilityContextCustomizer, PropertyMappingContextCustomizer, Customizer]
19:55:23.648 [main] DEBUG org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider - Identified candidate component class: file [D:\GitHub-Projects\Projects\Spring-Boot\APIs\SimpleAPI\rest-demo\target\classes\com\example\restdemo\RestDemoApplication.class]
19:55:23.649 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Found @SpringBootConfiguration com.example.restdemo.RestDemoApplication for test class com.example.restdemo.RestDemoApplicationTests
19:55:23.724 [main] DEBUG org.springframework.test.context.util.TestContextSpringFactoriesUtils - Skipping candidate TestExecutionListener [org.springframework.test.context.transaction.TransactionalTestExecutionListener] due to a missing dependency. Specify custom TestExecutionListener classes or make the default TestExecutionListener classes and their required dependencies available. Offending class: [org/springframework/transaction/interceptor/TransactionAttributeSource]
19:55:23.725 [main] DEBUG org.springframework.test.context.util.TestContextSpringFactoriesUtils - Skipping candidate TestExecutionListener [org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener] due to a missing dependency. Specify custom TestExecutionListener classes or make the default TestExecutionListener classes and their required dependencies available. Offending class: [org/springframework/transaction/interceptor/TransactionAttribute]
19:55:23.727 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Using TestExecutionListeners for test class [RestDemoApplicationTests]: [ServletTestExecutionListener, DirtiesContextBeforeModesTestExecutionListener, ApplicationEventsTestExecutionListener, MockitoTestExecutionListener, DependencyInjectionTestExecutionListener, DirtiesContextTestExecutionListener, EventPublishingTestExecutionListener, ResetMocksTestExecutionListener, RestDocsTestExecutionListener, MockRestServiceServerResetTestExecutionListener, MockMvcPrintOnlyOnFailureTestExecutionListener, WebDriverTestExecutionListener, MockWebServiceServerTestExecutionListener]
19:55:23.728 [main] DEBUG org.springframework.test.context.support.AbstractDirtiesContextTestExecutionListener - Before test class: class [RestDemoApplicationTests], class annotated with @DirtiesContext [false] with mode [null]
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.0.1)
2023-01-23T19:55:23.966+02:00 INFO 11748 --- [ main] c.e.restdemo.RestDemoApplicationTests : Starting RestDemoApplicationTests using Java 19.0.1 with PID 11748 (started by DELL-LAPTOP$ in D:\GitHub-Projects\Projects\Spring-Boot\APIs\SimpleAPI\rest-demo)
2023-01-23T19:55:23.967+02:00 INFO 11748 --- [ main] c.e.restdemo.RestDemoApplicationTests : No active profile set, falling back to 1 default profile: "default"
2023-01-23T19:55:24.913+02:00 INFO 11748 --- [ main] c.e.restdemo.RestDemoApplicationTests : Started RestDemoApplicationTests in 1.158 seconds (process running for 1.884)
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.847 s - in com.example.restdemo.RestDemoApplicationTests
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO]
[INFO] --- maven-jar-plugin:3.3.0:jar (default-jar) @ rest-demo ---
[INFO] Building jar: D:\GitHub-Projects\Projects\Spring-Boot\APIs\SimpleAPI\rest-demo\target\rest-demo-v1.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:3.0.1:repackage (repackage) @ rest-demo ---
[INFO] Replacing main artifact with repackaged archive
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.127 s
[INFO] Finished at: 2023-01-23T19:55:26+02:00
[INFO] ------------------------------------------------------------------------
Finished: SUCCESS
As you can see, Maven could be run from Jenkins.
This integration could be improved by using Maven Integration plugin.