#
Upload a file to Nexus
This tutorial explains to you how we can integrate Maven with Nexus repository. I will show how Maven can upload artifacts to Nexus repository.
Prerequisites for this demo:
- we have a Nexus repository named "nexus-repo-1" (format:
maven2
, type:hosted
) - we have a Spring Boot application which uses Maven
Here are the steps for integrating Maven with Nexus repository:
- take a loot at maven configuration
Run the following command:
mvn -X
This command will get a lot of information and errors, but we need to find in the message something like this:
[DEBUG] Reading global settings from C:\MySOFT\maven\apache-maven-3.8.7\conf\settings.xml
[DEBUG] Reading user settings from C:\Users\Catalin\.m2\settings.xml
Now we know which settings.xml
files are used by Maven.
Here are the steps for integrating Maven with Nexus:
- add Nexus information in settings.xml file
In my case I have no C:\Users\Catalin.m2\settings.xml file and I will create it.
I will add this information in it:
<settings xmlns="http://maven.apache.org/SETTINGS/1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd">
<servers>
<server>
<id>nexus-repo-1</id>
<username>admin</username>
<password>1</password>
</server>
</servers>
</settings>
Here we need to define a server, which is our repository id. The credentials for this server are put here as well.
- add the needed information in pom.xml file
I added the following in pom.xml file:
<distributionManagement>
<repository>
<id>nexus-repo-1</id>
<url>http://localhost:8081/repository/nexus-repo-1/</url>
</repository>
</distributionManagement>
Here you have the full content of my pom.xml file:
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>rest-demo</artifactId>
<version>v1</version>
<packaging>jar</packaging>
<name>rest-demo</name>
<description>Demo for Spring Boot REST API</description>
<properties>
<java.version>19</java.version>
</properties>
<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>
<!-- exclude junit 4 -->
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.10.1</version>
</dependency>
<!-- junit 5 -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<distributionManagement>
<repository>
<id>nexus-repo-1</id>
<url>http://localhost:8081/repository/nexus-repo-1/</url>
</repository>
</distributionManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${parent.version}</version>
</plugin>
</plugins>
</build>
</project>
Now Maven is integrated with Nexus. For testing this, we can go to the Spring Boot project directory and run the following command:
mvn deploy
For more information about Maven lifecycles, take a look here
Here is the log of the command:
[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-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] Nothing to compile - all classes are up to date
[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] Nothing to compile - all classes are up to date
[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
23:03:06.859 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Neither @ContextConfiguration nor @ContextHierarchy found for test class [RestDemoApplicationTests]: using SpringBootContextLoader
23:03:06.863 [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}.
23:03:06.863 [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.
23:03:06.885 [main] DEBUG org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Using ContextCustomizers for test class [RestDemoApplicationTests]: [ExcludeFilterContextCustomizer, DuplicateJsonObjectContextCustomizer, MockitoContextCustomizer, TestRestTemplateContextCustomizer, DisableObservabilityContextCustomizer, PropertyMappingContextCustomizer, Customizer]
23:03:06.950 [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]
23:03:06.952 [main] INFO org.springframework.boot.test.context.SpringBootTestContextBootstrapper - Found @SpringBootConfiguration com.example.restdemo.RestDemoApplication for test class com.example.restdemo.RestDemoApplicationTests
23:03:07.035 [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]
23:03:07.037 [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]
23:03:07.039 [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]
23:03:07.039 [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-25T23:03:07.307+02:00 INFO 12504 --- [ main] c.e.restdemo.RestDemoApplicationTests : Starting RestDemoApplicationTests using Java 19.0.1 with PID 12504 (started by Catalin in D:\GitHub-Projects\Projects\Spring-Boot\APIs\SimpleAPI\rest-demo)
2023-01-25T23:03:07.308+02:00 INFO 12504 --- [ main] c.e.restdemo.RestDemoApplicationTests : No active profile set, falling back to 1 default profile: "default"
2023-01-25T23:03:08.340+02:00 INFO 12504 --- [ main] c.e.restdemo.RestDemoApplicationTests : Started RestDemoApplicationTests in 1.272 seconds (process running for 3.199)
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.037 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]
[INFO] --- spring-boot-maven-plugin:3.0.1:repackage (repackage) @ rest-demo ---
[INFO] Replacing main artifact with repackaged archive
[INFO]
[INFO] --- maven-install-plugin:3.0.1:install (default-install) @ rest-demo ---
[INFO] Installing D:\GitHub-Projects\Projects\Spring-Boot\APIs\SimpleAPI\rest-demo\pom.xml to C:\Users\Catalin\.m2\repository\com\example\rest-demo\v1\rest-demo-v1.pom
[INFO] Installing D:\GitHub-Projects\Projects\Spring-Boot\APIs\SimpleAPI\rest-demo\target\rest-demo-v1.jar to C:\Users\Catalin\.m2\repository\com\example\rest-demo\v1\rest-demo-v1.jar
[INFO]
[INFO] --- maven-deploy-plugin:3.0.0:deploy (default-deploy) @ rest-demo ---
Uploading to nexus-repo-1: http://localhost:8081/repository/nexus-repo-1/com/example/rest-demo/v1/rest-demo-v1.pom
Uploaded to nexus-repo-1: http://localhost:8081/repository/nexus-repo-1/com/example/rest-demo/v1/rest-demo-v1.pom (2.1 kB at 9.7 kB/s)
Uploading to nexus-repo-1: http://localhost:8081/repository/nexus-repo-1/com/example/rest-demo/v1/rest-demo-v1.jar
Uploaded to nexus-repo-1: http://localhost:8081/repository/nexus-repo-1/com/example/rest-demo/v1/rest-demo-v1.jar (19 MB at 35 MB/s)
Downloading from nexus-repo-1: http://localhost:8081/repository/nexus-repo-1/com/example/rest-demo/maven-metadata.xml
Uploading to nexus-repo-1: http://localhost:8081/repository/nexus-repo-1/com/example/rest-demo/maven-metadata.xml
Uploaded to nexus-repo-1: http://localhost:8081/repository/nexus-repo-1/com/example/rest-demo/maven-metadata.xml (294 B at 4.7 kB/s)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 11.118 s
[INFO] Finished at: 2023-01-25T23:03:12+02:00
[INFO] ------------------------------------------------------------------------
Here you can see the phases maven has been executed. The last phase is deploy.
When we go to Nexus repository we can see the content uploaded by Maven: