How to create own Maven Repository? - maven

I want to know, how to create my own dependency to use my code in other projects.
I were following tutorial.
I`ve tried to create project with simple class as Maven Project.
I did clean-package. Created github repository. Added my project there with "target" package.
in pom.xml i added
<properties>
<java.version>1.8</java.version>
<github.global.server>github</github.global.server>
<github.maven-plugin>0.12</github.maven-plugin>
</properties>
<distributionManagement>
<repository>
<id>internal.repo</id>
<name>Temporary Staging Repository</name>
<url>file://${project.build.directory}/mvn-repo</url>
</repository>
</distributionManagement>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.1</version>
<configuration>
<altDeploymentRepository>internal.repo::default::file://${project.build.directory}/mvn-repo</altDeploymentRepository>
</configuration>
</plugin>
<plugin>
<groupId>com.github.github</groupId>
<artifactId>site-maven-plugin</artifactId>
<version>${github.maven-plugin}</version>
<configuration>
<message>Maven artifacts for ${project.version}</message>
<noJekyll>true</noJekyll>
<outputDirectory>${project.build.directory}/mvn-repo</outputDirectory>
<branch>refs/heads/mvn-repo</branch>
<includes><include>**/*</include></includes>
<repositoryName>GITHUB_NAME_REPOSITORY</repositoryName>
<repositoryOwner>MY_GITHUB_NICKNAME</repositoryOwner>
</configuration>
<executions>
<execution>
<goals>
<goal>site</goal>
</goals>
<phase>deploy</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
after in root of .m2 directory i created settings.xml with:
<settings>
<servers>
<server>
<id>github</id>
<username>[username]</username>
<password>[password]</password>
</server>
</servers>
</settings>
did again clean+package and pushed to github.
after trying to use dependency - not found.
in github repo no mvn-repo branch

I haven't used the new GitHub repositories yet, but what work quite well so far:
private, single machine usage: mvn install -> the artifact will be installed in your local Maven repository and can be referenced by any other project on the same machine
Open Source, multiple machines/ developers: mvn deploy to Maven Central. See the documentation for more information about configuration and involved steps.
Closed Source, multiple machines/ developers: mvn deploy to your own Maven Repository manager such as Nexus (configure the distributionManagement accordingly)
That said, it's a best practice to use your own Maven Repository Manager in all 3 cases and define a single group.
From the Maven default lifecycle documentation:
package: take the compiled code and package it in its distributable format, such as a JAR.
install: install the package into the local repository, for use as a dependency in other projects locally.
deploy: done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.

Related

Unable to use class from Custom Spring boot library

Goal
I want to use a private repository as a library in other spring boot projects by hosting it as a GitHub package.
Library project
https://github.com/sagarnayak/privatecentralrepo
Client Project
https://github.com/sagarnayak/repoclientproject
Steps To Reproduce
the library project has a library001 module. this is what I want to use as a library for other projects.
In the library module pom file, I have added the repackage execution goal.
......
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>exec</classifier>
</configuration>
</execution>
</executions>
</plugin>
......
I want to use the module as a library and host it as a private GitHub package.
When I do the mvnw deploy in the library001 module this should create an exec jar and push to GitHub to use this library in other projects.
Github has this exec jar.
To use the jar in the client project I have added this as a dependency in the client project.
<dependency>
<groupId>com.sagar</groupId>
<artifactId>libraray001</artifactId>
<version>0.0.3-SNAPSHOT</version>
<classifier>exec</classifier>
</dependency>
This gets the project into the external libraries part of the client project. and this has the class that I want to use in the library (TestModel001).
But when I try to import this into any classes that I want to use it can not resolve the import.
How do I use the library project in this project?
I solved the issue with this -
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>repackage</id>
<configuration>
<classifier>exec</classifier>
</configuration>
</execution>
</executions>
</plugin>
The issue was with the pom file.
I was using the configurations for an older version.
The client project as visible at sagarnayak/repoclientproject is not configured to consume the artifacts from the private Maven repository hosted on GitHub as described in the Working with the Apache Maven registry.
Specifically, following settings are missing:
<repositories> configuration in pom.xml
<servers> configuration in local settings.xml
Add following elements to the pom.xml:
<repositories>
<repository>
<id>github</id>
<url>https://maven.pkg.github.com/sagarnayak/</url>
</repository>
</repositories>
Add following elements to the settings.xml
<servers>
<server>
<id>github</id>
<username>sagarnayak</username>
<password>YOUR_PERSONAL_ACCESS_TOKEN_HERE</password>
</server>
</servers>
Note: Keep settings.xml local to your machine - don't leak that Personal Access Token!
You are packaging the library via spring-boot-maven-plugin
It produces a JAR that has a self-running spring boot structure, and so you are unable to import it as a library correctly.
Try to remove spring-boot-maven-plugin from your libraray001 pom.xml, rebuild, republish and try to consume that artifact in your client project again, refreshing the dependencies and preferably bumping up the library version, to exclude caching possibility during the test.

Generate and update changelog.md after mvn release:perform

I would like to generate, update changelog.md and commit to Bitbucket repository after mvn release:perform is done.
In angular, it has #semantic-release/git, #semantic-release/commit-analyzer, #semantic-release/release-notes-generator, #semantic-release/npm, #semantic-release/changelog, #semantic-release/exec. Is there anything similar in maven release?
pom.xml
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>3.0.0-M1</version>
<configuration>
<tagNameFormat>v#{project.version}</tagNameFormat>
<checkModificationExcludes>
<checkModificationExclude>pom.xml</checkModificationExclude>
</checkModificationExcludes>
</configuration>
</plugin>
</plugins>
</build>
<distributionManagement>
<repository>
<id>release</id>
<url>https://nexus.../.../release/</url>
</repository>
</distributionManagement>
<properties>
<project.scm.id>my-scm-server</project.scm.id>
</properties>
<scm>
<connection>scm:git:https://user#bitbucket.org/user/comutils.git</connection>
<developerConnection>scm:git:https://user#bitbucket.org/user/comutils.git</developerConnection>
<tag>HEAD</tag>
</scm>
You can use maven-semantic-release which will generate release notes using Angular style commit messages for Maven projects in the same way as you would do with npm projects.
The documentation suggests how to publish to Maven Central, but you can save some hassle by publishing to Github packages.

How can I make a pom.xml use local properties specific to each developer's local environment?

We have a maven project on our subversion server. There is a pom.xml inside the project. When I want to import project to my local computer and make my changes on it, I have to change this lines relative to my local system inside the pom.xml because I need to deploy project on my local system to test it before submit:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<webappDirectory>/opt/JBoss_4.0.4.GA/server/default/deploy/agg-gateway.war</webappDirectory>
</configuration>
</plugin>
Each of my team members should do that relative to their local systems.
But this settings must be unchanged on main project an I have not to submit pom.xml to subversion. If one of my team members changes this file, every time I update the project, I have to change it again relative to my local system.
Is there any way to make a file unchangeable on subversion? I can not control everybody to not to submit pom.xml to svn.
Injecting POM Properties via settings.xml:
Use a custom property in your ~/.m2/settings.xml which is specific to each developer:
<profiles>
<profile>
<id>local-weblogic</id>
<properties>
<local.weblogic.deployment.directory>/opt/JBoss_4.0.4.GA/server/default/deploy/agg-gateway.war</local.weblogic.deployment.directory>
</properties>
</profile>
</profiles>
<activeProfiles>
<activeProfile>local-weblogic</activeProfile>
</activeProfiles>
and use a reference to the property in your pom.xml
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.4</version>
<configuration>
<webappDirectory>${local.weblogic.deployment.directory}</webappDirectory>
</configuration>
</plugin>

Not downloading converted OSGI bundles from custom p2 site using Maven

My project is in RCP. The product is created using Maven. My RCP project depends on third party jars. Currently we are adding these jars in plugin.xml "runtime". So whenever there is change in third party jars, we have to update the plugin.xml file.
Now we are changing the process by converting the third party jars in to OSGI bundle using maven then create p2 site and while building, add the third party OSGI bundles in classpath.
We have done the conversion to OSGI bundle and creating p2 site. But when we are adding it in repository, the converted third party OSGI bundles are not always get download.
We have added below code in pom.xml to convert in to OSGI bundle and create p2:site:
<build>
<plugins>
<plugin>
<groupId>org.reficio</groupId>
<artifactId>p2-maven-plugin</artifactId>
<version>1.2.0-SNAPSHOT</version>
<executions>
<execution>
<id>default-cli</id>
<configuration>
<artifacts>
<artifact><id>com.test.proj:proj-jar1:6.01.00-SNAPSHOT</id></artifact>
<artifact><id>com.test.proj:proj-jar2:6.01.00-SNAPSHOT</id></artifact>
<artifact><id>com.test.proj:proj-jar3:1.1</id></artifact>
<artifact><id>com.test.proj:proj-jar4:1.0</id></artifact>
<artifact><id>com.test.proj:proj-jar5:1.0</id></artifact>
<artifact><id>com.test.proj:proj-jar6:6.01.00-SNAPSHOT</id></artifact>
</artifacts>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>8.1.5.v20120716</version>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<webAppSourceDirectory>${basedir}/target/repository/</webAppSourceDirectory>
<webApp>
<contextPath>/site</contextPath>
</webApp>
</configuration>
</plugin>
<plugin>
<groupId>org.eclipse.tycho</groupId>
<artifactId>tycho-maven-plugin</artifactId>
<version>0.22.0</version>
<extensions>false</extensions>
</plugin>
</plugins>
</build>
<pluginRepositories>
<pluginRepository>
<id>reficio</id>
<url>http://repo.reficio.org/maven/</url>
</pluginRepository>
</pluginRepositories>
To download while build we have added:
<repositories>
<repository>
<id>extJars</id>
<url>http://localhost:8080/site/</url>
<layout>p2</layout>
</repository>
</repositories>
When we call "mvn install" it is not downloading the jars from "http://localhost:8080/site" every time.
So please let me know what is going wrong in my pom.xml
Any help is appreciated. Thanks
In Maven you cannot build something and then depend on it dynamically in one reactor. This way the build dependencies cannot be computed. You either have to
Build your p2 update site
Build the rest
or you should use something the m2e people call wrapper bundle. They used it in this pom.xml.

Deploy Maven project to local Artifactory service

I have forked a webjar project for working locally in my company's environment. We use Artifactory/Ivy for dependency management.
Currently Smart Table (and other webjars) pom.xml show the following for deployment:
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.5</version>
<extensions>true</extensions>
<configuration>
<serverId>sonatype-nexus-staging</serverId>
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>
It will by default publish to Sonatype, which is good for publicly-visible open source projects once you have release credentials.
However we do currently want to work locally on a fork of the project and deploy to our local Artifactory server. Contributions (to the real project) will be shared via Pull Request, so we are not interested in going to Sonatype repository.
Question
How do I change Maven pom.xml so that mvn deploy will deploy to a locally-configured Artifactory service? (For which credentials are stored in Maven configuration of course)
Bonus question
Can I tell Maven to publish using Ivy layout or should I create a new Maven-layout repository in Artifactory?
First option is to use the standard Maven deploy plugin
<distributionManagement>
<repository>
<id>repo-id</id>
<name>Artifactory</name>
<url>http://server:8081/artifactory/repo-id</url>
</repository>
</distributionManagement>
You should configure your settings.xml file to define corresponding entries which provides authentication information. Server entries are matched to the different parts of the distributionManagement using their elements.
<server>
<id>repo-id</id>
<username>repo-username</username>
<password>password/encrypted password</password>
</server>
Second option is to use the JFrog Maven Artifactory plugin, available at the JCenter repository in Bintray
<build>
<plugins>
...
<plugin>
<groupId>org.jfrog.buildinfo</groupId>
<artifactId>artifactory-maven-plugin</artifactId>
<version>2.4.0</version>
<inherited>false</inherited>
<executions>
<execution>
<id>build-info</id>
<goals>
<goal>publish</goal>
</goals>
<configuration>
<deployProperties>
<gradle>awesome</gradle>
<review.team>qa</review.team>
</deployProperties>
<publisher>
<contextUrl>https://server:8081/artifactory</contextUrl>
<username>username</username>
<password>{DESede}...</password>
<repoKey>libs-release-local</repoKey>
<snapshotRepoKey>libs-snapshot-local</snapshotRepoKey>
</publisher>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
Through the Maven Artifactory Plugin, Artifactory is fully integrated with Maven builds and allows you to do the following:
Attach properties to published artifacts in Artifactory metadata.
Capture a BuildInfo object which can be passed to the Artifactory REST API to provide a fully traceable build context.
Automatically publish all build artifacts at the end of the build.
More detailed usage examples of the plugin can be found in this Github project.
Bonus question
Maven can only deploy to a Maven2 (default) or Maven1 (legacy) layout repository. You will have to create a new Maven repository in Artifactory.

Resources