Unable to use class from Custom Spring boot library - spring-boot

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.

Related

How to create own Maven Repository?

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.

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.

POM error in Spring STS trying to set-up Spring Integration Sample

Spring Boot / Spring Integration Sample Project: cafe-dsl
Error Message:
Missing artifact org.springframework.integration.samples:cafe-si:jar:4.1.0.BUILD-SNAPSHOT
Description:After installing Spring STS (3.7) and installing the maven(3.3) project cafe-dsl. I get the error message above. I am using the maven embedded with Spring STS. Is there something I need to configure additionally so that it will properly build the project?
GIT Repo: https://github.com/spring-projects/spring-integration-samples.git
As you this project (cafe-dsl) requires another Spring Integration Sample project:
<dependency>
<groupId>org.springframework.integration.samples</groupId>
<artifactId>cafe-si</artifactId>
<version>4.1.0.BUILD-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
Since it is a BUILD-SNAPSHOT and you'd like do not open the entire Spring Integration Samples project you just should add snapshot repo to the POM:
<repository>
<id>repo.spring.io.snapshot</id>
<name>Spring Framework Maven Snapshot Repository</name>
<url>https://repo.spring.io/libs-snapshot</url>
</repository>
UPDATE
The previous expression regarding the libs-snapshot and relying on the cafe-si from there was wrong. Since we don't install those samples projects as artifacts to the Maven repository it won't work that way.
I can confirm that it works only after these manipulations:
gredlew install from the root of Spring Integration Samples
This modifications to the cafe-dsl POM:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>org.springframework.integration.samples.dsl.cafe.lambda.Application</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.3</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
And this Maven command:
mvn spring-boot:run
Even if we can fix the POM generation from project perspective, I'm not sure that will install those artifacts to the libs-snapshot repo afterwards...
How about just rely on the Gradle?

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.

how to add external jar to maven webapp project

I have a Spring roo project (basically a maven project). I want to add dropbox sdk to the project, problem is it's not in maven. I added the following files
<dependency>
<groupId>com.dropbox</groupId>
<artifactId>dropbox-sdk</artifactId>
<version>1.3.1</version>
<scope>system</scope>
<systemPath>${project.basedir}/libs/dropbox-java-sdk-1.3.1.jar</systemPath>
</dependency>
It solved the compile error, but when i run the project, in Spring Tool Suite, the jar files are not added to war lib folder. How do I make maven add my external jar files to my the war lib folder?
I don't want to install the jar in maven since, I have to install it in all the machines that uses the project
I finally found a neat solution, which is a lot easier to implement. You add an in-project repository inside the java project and link to it in the pom.
You add an in-project repository in maven like this:
<repository>
<id>in-project</id>
<name>In Project Repo</name>
<url>file://${project.basedir}/libs</url>
</repository>
Then create a folder structure in the root folder of your project that looks something like this
/groupId/artifactId/version/artifactId-version.jar
and add the dependency as you would normally do.
This approach has the least amount of code and work required, and if that library ever gets add into a maven repository you can always remove your in-project repository.
There is a much easier solution, which is set webResource in the plugin. By the solution, you can add any files of your local disk to the war! A sample is as below,
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<warName>api</warName>
<webResources>
<resource>
<directory>libs/</directory>
<targetPath>WEB-INF/lib</targetPath>
<includes>
<include>**/*.jar</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
The best way to resolve this issue is to add these local jar files to WEB-INF/lib folder. You will find all these jars packaged in your final war file then.
I don't recommend this approach, but you could add some POM configuration to install the 3rd-party dependency in a separate profile:
<profiles>
<profile>
<id>install-dependencies</id>
<build>
<plugins>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.3.1</version>
<executions>
<execution>
<id>install-dropbox-sdk</id>
<phase>validate</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<groupId>com.dropbox</groupId>
<artifactId>dropbox-sdk</artifactId>
<version>1.3.1</version>
<file>src/main/lib/dropbox-java-sdk-1.3.1.jar</file>
<packaging>jar</packaging>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>build</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<dependencies>
<dependency>
<groupId>com.dropbox</groupId>
<artifactId>dropbox-sdk</artifactId>
<version>1.3.1</version>
</dependency>
</dependencies>
</profile>
</profiles>
There are two profiles here: install-dependencies and build. The first installs the dropbox-sdk dependency into your Maven repository and needs to be run once on every machine as follows:
mvn -Pinstall-dependencies validate
The second is enabled by default, and adds the Dropbox SDK as a dependency.
To be honest though, this isn't much better than running
mvn install:install-file -Dfile=src/main/lib/dropbox-java-sdk-1.3.1.jar -DgroupId=com.dropbox -DartifactId=dropbox-sdk -Dversion=1.3.1 -Dpackaging=jar
on every machine.
The other downside of this approach is that you'll have to add all dependencies of the dropbox-sdk to your build as well- whereas if it is done properly by adding the JAR and a POM to a repository server, then Maven will calculate the transitive dependencies properly.
I recommend creating a "third party" repository in a Maven repository server such as Nexus or Artifactory, and uploading the jar to there. Even though that means putting the jar into Maven, at least with a repository server it is available to anyone who will be building your application.
change the lib path to :
src/main/webapp/WEB-INF/lib
in pom.xml:
<systemPath>${project.basedir}/src/main/webapp/WEB-INF/lib/xxxx.jar</systemPath>
The steps described in this site are pretty simple, and they work well enough:
https://mythinkpond.com/2010/10/02/adding-custom-jars-under-web-inflib-in-a-maven-project/
Create a “lib” folder under your project like this: “\src\main\webapp\WEB-INF\lib”
Copy needed “jars” etc that you want included inside your WAR bundle folder.
Invoke your maven build as you normally do. I use “mvn install”, which creates builds the war file.
I know I am really late but I was wondering on why you would not put in the jar in the local repo in the .m2 file and add a reference to the pom from there ?

Resources