I have used this article (https://gist.github.com/fernandezpablo85/03cf8b0cd2e7d8527063) for creating a custom maven repo, and it works. But now I have a problem with a private repo. How can I specify credentials for the private repo?
You can set in .m2/settings.xml file
Like This:
<settings>
<servers>
<server>
<id>private-repo</id>
<username>xyz</username>
<password>${pass}</password>
</server>
</servers>
</settings>
And in pom.xml:
<project>
...
<repositories>
<repository>
<id>private-repo</id>
<url>${private-repo.url}</url>
</repository>
</repositories>
...
</project>
A settings.xml file is usually found in a couple of places:
Global settings in Mavens home directory: ${maven.home}/conf/settings.xml
User settings in the user’s home: ${user.home}/.m2/settings.xml
If both files exist, their contents are merged. Configurations from the user settings take precedence.
If your settings doesn't apply you can try to override the default settings.xml file location by adding --settings flag to your mvn ... command.
sources:
https://www.baeldung.com/maven-settings-xml#settings-level
https://www.baeldung.com/maven-settings-xml#3-override-the-default-location
Related
I have a maven project with a few custom dependencies that reside on a private repository. I'm attempting to create a docker image based on one of the maven images with these dependencies pre-loaded into the local maven repository.
Dockerfile
FROM maven:3.8.6-openjdk-11-slim
COPY settings-docker.xml /usr/share/maven/ref/
COPY bom.xml /tmp
RUN mvn -B -f /tmp/bom.xml -s /usr/share/maven/ref/settings-docker.xml dependency:resolve
settings-docker.xml
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
https://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository>/usr/share/maven/ref/repository</localRepository>
<mirrors>
<mirror>
<id>Mirror of Private Repo</id>
<mirrorOf>Private Repo</mirrorOf>
<name>allows http</name>
<url>http://here.it.is/repository/</url>
</mirror>
</mirrors>
</settings>
bom.xml
<?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 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.myproject</groupId>
<artifactId>bom</artifactId>
<packaging>pom</packaging>
<version>1.0</version>
<repositories>
<repository>
<id>Private Repo</id>
<url>http://here.it.is/repository/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>codec</groupId>
<artifactId>codec</artifactId>
<version>1.10.0.</version>
</dependency>
</dependencies>
</project>
I followed the instructions from the official maven image on using /usr/share/maven/ref/repository for preloaded dependencies. The image builds successfully and when I start it I can see my dependency in both /usr/share/maven/ref/repository and in /root/.m2/repository.
Despite this when I run maven, it will always attempt to connect to my private repository. Is this some maven behavior I don't know about or is it ignoring my repository?
I believe you have faced with "enhanced local repository manager" feature:
Enhanced local repository manager is built upon the classical Maven
2.0 local repository structure but additionally keeps track of from what repositories a cached artifact was resolved. Resolution of
locally cached artifacts will be rejected in case the current
resolution request does not match the known source repositories of an
artifact, thereby emulating physically separated artifact caches per
remote repository.
For example:
% cat ~/.m2/repository/org/springframework/spring-core/5.3.9/_remote.repositories
#NOTE: This is a Maven Resolver internal implementation file, its format can be changed without prior notice.
#Wed Mar 16 08:49:28 AEDT 2022
spring-core-5.3.9.pom>internal-repository=
spring-core-5.3.9.pom>central=
spring-core-5.3.9.jar>central=
spring-core-5.3.9.jar>internal-repository=
You may either disable that feature via specifying -llr in maven opts or investigate how force maven to use the same "repository id" in different scenarios.
I need to set flyway migration and I don't want to put password and username in pom.xml, I created flyway.properties file, but it's not working, I'm getting this error
Failed to execute goal org.flywaydb:flyway-maven-plugin:6.5.1:clean (default-cli) on project entesting: org.flywaydb.core.api.FlywayException: Unable to connect to the database. Configure the url, user and password!
flyway.properties file is in same directory as pom.xml
flyway.user=sa
flyway.password=passwordForThis
flyway.url=jdbc:sqlserver://172.23.176.144;database=DB_Name
flyway.locations=filesystem:src/main/resources/db/migration
flyway.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
pom.xml flyway plugin config:
<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>6.5.1</version>
<configuration>
<url>jdbc:sqlserver://172.23.176.144;database=DB_Name</url>
<user>sa</user>
<password>passwordForThis</password>
</configuration>
</plugin>
To summarize I don't want to add password, username etc. in pom.xml(that works), I want it to be in flyway.propeties.
Flyway can be configured in a number of ways beyond directly putting it into the maven pom.xml. See documentation on configuring the maven plugin
The contents of the properties file you showed looks like the contents of a flyway.conf file.
Flyway will search for and automatically load the <user-home>/flyway.conf config file if present.
It is also possible to point Flyway at one or more additional config files. This is achieved by supplying the System property flyway.configFiles as follows:
mvn -Dflyway.configFiles=path/to/myAlternativeConfig.conf flyway:migrate
See https://flywaydb.org/documentation/maven/#config-files for more information.
Alternatively for storing the database user and password, Maven settings.xml files can also be used:
<settings>
<servers>
<server>
<!-- By default Flyway will look for the server with the id 'flyway-db' -->
<!-- This can be customized by configuring the 'serverId' property -->
<id>flyway-db</id>
<username>myUser</username>
<password>mySecretPwd</password>
</server>
</servers>
</settings>
I have a Maven project and want to build it using mvn clean deploy so that the built artifact is deployed into a Nexus repository.
The access data (username and password) for that repository are stored in Jenkins credentials.
I want to call mvn deploy in Jenkins so that the credentials for that Nexus repository are read from Jenkins (not hardcoded in settings.xml).
How can I do it, if I cannot access settings.xml on the Jenkins server?
Update 1:
I created an entry in "Config File Management" (JENKINS_URL/configfiles/index) with following data:
Type: Maven settings.xml
Replace All: Yes
Server ID: myServer
Credentials: Credentials for the Nexus repository
Content:
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<servers>
<server>
<id>myServer</id>
<username>foo</username>
<password>bar</password>
</server>
</servers>
</settings>
myServer is also used in the pom.xml of the artifact I want to build:
<distributionManagement>
<repository>
<id>myServer</id>
<url>http://nexus.mycompany.com</url>
</repository>
</distributionManagement>
In the configuration of the job, I include those settings as shown below. Nexus settings.xml is the configuration from "Config File Management".
But it does not work -- I get "Forbidden" error when the build attempts to deploy artifacts to Nexus.
Update 2: When I run mvn -X deploy locally with the same credentials as in Jenkins (stored in my local settings.xml), I see following output:
[DEBUG] Failed to decrypt password for server XXX release repository: org.sonatype.plexus.components.sec.dispatcher.SecDispatcherException: java.io.FileNotFoundException: /XXXXXXXXXXXXXXX/.m2/settings-security.xml (No such file or directory)
org.sonatype.plexus.components.sec.dispatcher.SecDispatcherException: org.sonatype.plexus.components.sec.dispatcher.SecDispatcherException: java.io.FileNotFoundException: /XXXXXXXXXXXXXXX/.m2/settings-security.xml (No such file or directory)
at org.sonatype.plexus.components.sec.dispatcher.DefaultSecDispatcher.decrypt(DefaultSecDispatcher.java:121)
at org.apache.maven.settings.crypto.DefaultSettingsDecrypter.decrypt(DefaultSettingsDecrypter.java:107)
at org.apache.maven.settings.crypto.DefaultSettingsDecrypter.decrypt(DefaultSettingsDecrypter.java:63)
at org.apache.maven.internal.aether.DefaultRepositorySystemSessionFactory.newRepositorySession(DefaultRepositorySystemSessionFactory.java:165)
However, the password in settings.xml is not encrypted at all:
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
https://maven.apache.org/xsd/settings-1.0.0.xsd">
<localRepository/>
<interactiveMode/>
<offline/>
<pluginGroups/>
<servers>
<server>
<id>NEXUS_REPOSITORY_SNAPSHOTS</id>
<username>user</username>
<password>password</password>
</server>
</servers>
<mirrors/>
<proxies/>
<profiles/>
<activeProfiles/>
</settings>
I found a report about similar error here.
You can use the Jenkins config-file-provider plugin ( link: https://plugins.jenkins.io/config-file-provider/ ) to create one or several Maven settings.xml files.
NOTE: Really this helps also if you need differents settings.xml for
different projects.
Then on the maven deploy step of your Jenkins project you can select to point to one of the settings.xml files defined ( instead of pointing to the general Jenkins server /.m2/settings.xml )
I am struggling with the (seemingly) simple task of deploying a maven project to github packages using a github actions workflow. First of all, here's the error I am getting in the maven deploy phase:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy (default-deploy) on project [project name]: Failed to retrieve remote metadata [groupId]:[artifactId]:1.0-SNAPSHOT/maven-metadata.xml: Could not transfer metadata [groupId]:[artifactId]:1.0-SNAPSHOT/maven-metadata.xml from/to github (https://maven.pkg.github.com/[username]/[repository]): Failed to transfer file https://maven.pkg.github.com/[username]/[repository]/[groupId as path]/[artifactId]/1.0-SNAPSHOT/maven-metadata.xml with status code 400 -> [Help 1]
(Info: I replaced unneccessary and/or private concrete information with general terms in [brackets])
Most likely, the actual maven-metadata.xml file is not the problem because I have already seen warnings like "could not upload checksum" with status 400 before. I guess that maven-metadata.xml is just the first file it fails on, but probably I am completely wrong with this assumption, please tell me if so.
Probably the most important file is the workflow yaml file:
name: Deploy SNAPSHOT (develop)
on:
push:
branches:
- develop
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v1
- name: Set up JDK 11
uses: actions/setup-java#v1
with:
java-version: 11
- name: Maven Deploy
env:
GITHUB_USERNAME: x-access-token
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: mvn --settings settings.xml -B -e -Dmaven.wagon.http.pool=false clean deploy
Also quite important: the maven settings.xml file:
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<activeProfiles>
<activeProfile>github-packages</activeProfile>
</activeProfiles>
<profiles>
<profile>
<id>github-packages</id>
<repositories>
<repository>
<id>central</id>
<url>https://repo1.maven.org/maven2/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>github</id>
<name>GitHub [username] Apache Maven Packages</name>
<url>https://maven.pkg.github.com/[username]</url>
</repository>
</repositories>
</profile>
</profiles>
<servers>
<server>
<id>github</id>
<username>${env.GITHUB_USERNAME}</username>
<password>${env.GITHUB_TOKEN}</password>
</server>
</servers>
</settings>
(Info: same goes for values in brackets as before)
And lastly, the parent pom.xml of my maven project:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>[groupId]</groupId>
<artifactId>[artifactId]</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
[child modules]
</modules>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.release>11</maven.compiler.release>
</properties>
<dependencies>
[my dependencies]
</dependencies>
<distributionManagement>
<repository>
<id>github</id>
<name>GitHub [username] Apache Maven Packages</name>
<url>https://maven.pkg.github.com/[username]/[repository]</url>
</repository>
</distributionManagement>
</project>
Maybe it's also important to say that the GitHub repository belongs entirely to me and therefore I should have all admin rights on it.
Things I've tried:
I have done some research by now and I found that my issue seems to be not uncommon. Although, from all solutions I've found so far, not one has worked.
Official GitHub documentation for setting up workflows
To start of, I used this site as my reference.
This StackOverflow Question
I mostly stuck to all the advice I found in this question and in the answers to it.
This other StackOverflow Question about nexus deploys
The accepted answer of this question provides a checklist. I tried to check that all the bullet points work for me, although I wasn't able to validate everything. I did not find any issue based on this checklist.
This question on the GitHub community forum
The error displayed in this question looks very much like the error I am getting, still the proposed solution did not fix anything for me.
This Answer on a similar GitHub community forum question
This answer suggested using a personal access token instead of the GITHUB_TOKEN. I tried that and it changed nothing.
What I need
Of course I'd be happy if someone can show me what the exact issue of my case is, but it does not need to be that specific. I am trying to set up the most basic pipeline possible and I currently don't want to do more than just deploy a maven snapshot repository to github packages (with github actions). So, if anyone can show me how to do this properly in general, that's also fine. Thanks!
I kinda figured it out myself, but the result is very dissatisfying. The main problem here is:
Note: GitHub Packages does not support SNAPSHOT versions of Apache Maven. Make sure you disable SNAPHOT in your ~/.m2/settings.xml file.
(Source)
So, it seems like GitHub Packages does not support the construct of maven snapshot repositories, which are otherwise very practical. My plan was to set up a workflow that deploys a new SNAPSHOT on the package registry when a push to develop happens. Maven snapshot repositories do not require unique version numbers for deploys, which means I could have built a new version for every push, but the actual dependency version stays fixed. Everyone could then easily try out the latest state of my develop branch, simply by including the fixed snapshot version in their pom files.
Now, how to fix my issue:
Because GitHub packages does not support snapshot repositories, you will have to remove the keyword "SNAPSHOT" from the value in the project.version tag in your pom file. Basically you can put every other version description there, but now it is unique. However if you remove all SNAPSHOT keywords, the workflow should properly deploy a package to the GitHub package registry, at least it worked for me.
If anyone knows a way to "hack" around this SNAPSHOT issue, please tell me. Otherwise this is the only working solution I've found so far.
I have auto-generated java code I want to deploy in our maven repo. The generated code has a pom.xml without the repo info. I what to know if I can deploy the complete maven project in my repo without touching the pom. And by complete maven project, I mean not only the final jar using the file-deploy maven command.
The repo has security enabled and requires usr/pass
I found a way. You need to add the server to the settings file as following:
<servers>
<server>
<id>my-server</id>
<username>user</username>
<password>pass</password>
</server>
</servers>
then I deploy using the following maven command (replacing <url> for the target repo):
mvn deploy -D"altDeploymentRepository=my-server::default::<url>"