How do I force Maven to use my local repository rather than going out to remote repos to retrieve artifacts? - maven

I’m using Maven 3.3.3 with Java 8 on Mac Yosemite. I have a multi-module project.
<modules>
<module>first-module</module>
<module>my-module</module>
…
</modules>
When I build my one of my child modules, for example, “my-module” from above, using “mvn clean install”, the build attempts to download the child module artifacts from a remote repository I have defined in my ~/.m2/settings.xml file. Output is below
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building my-module 87.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
Downloading: https://my.remoterepository.com/nexus/content/repositories/snapshots/org/mainco/subco/first-module/87.0.0-SNAPSHOT/maven-metadata.xml
Downloading: http://download.java.net/maven/2/org/mainco/subco/first-module/87.0.0-SNAPSHOT/maven-metadata.xml
Downloaded: https://my.remoterepository.com/nexus/content/repositories/snapshots/org/mainco/subco/first-module/87.0.0-SNAPSHOT/maven-metadata.xml (788 B at 0.9 KB/sec)
Downloading: https://my.remoterepository.com/nexus/content/repositories/snapshots/org/mainco/subco/first-module/87.0.0-SNAPSHOT/first-module-87.0.0-20151104.200545-4.pom
How do I force Maven to check my local ~/.m2/repository first before trying to download from the remote repositories? Below is where I have my remote repositories defined in my ~/.m2/settings.xml file …
<profile>
<id>releases</id>
<activation>
<property>
<name>!releases.off</name>
</property>
</activation>
<repositories>
<repository>
<id>releases</id>
<url>https://my.remoterepository.com/nexus/content/repositories/releases/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</profile>
<profile>
<id>snapshots</id>
<activation>
<property>
<name>!snapshots.off</name>
</property>
</activation>
<repositories>
<repository>
<id>snapshots</id>
<url>https://my.remoterepository.com/nexus/content/repositories/snapshots/</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</profile>
Edit: In response to the answer saying that the download occurs when the artifact is not there, below is the terminal output in which I prove the file was there in my repo but Maven is trying to download it anyway ...
Daves-MacBook-Pro-2:my-module davea$ ls -al ~/.m2/repository/org/mainco/subco/first-module/87.0.0-SNAPSHOT/first-module-87.0.0-SNAPSHOT.jar
-rw-r--r-- 1 davea staff 10171 Nov 5 10:22 /Users/davea/.m2/repository/org/mainco/subco/first-module/87.0.0-SNAPSHOT/first-module-87.0.0-SNAPSHOT.jar
Daves-MacBook-Pro-2:my-module davea$ mvn clean install
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for org.mainco.subco:my-module:jar:87.0.0-SNAPSHOT
[WARNING] 'build.plugins.plugin.(groupId:artifactId)' must be unique but found duplicate declaration of plugin org.apache.maven.plugins:maven-antrun-plugin # org.mainco.subco:my-module:[unknown-version], /Users/davea/Documents/sb_workspace/my-module/pom.xml, line 678, column 12
[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] ------------------------------------------------------------------------
[INFO] Building my-module 87.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
Downloading: https://my.remoterepository.com/nexus/content/repositories/snapshots/org/mainco/subco/first-module/87.0.0-SNAPSHOT/maven-metadata.xml
Downloading: http://download.java.net/maven/2/org/mainco/subco/first-module/87.0.0-SNAPSHOT/maven-metadata.xml
Downloaded: https://my.remoterepository.com/nexus/content/repositories/snapshots/org/mainco/subco/first-module/87.0.0-SNAPSHOT/maven-metadata.xml (788 B at 0.8 KB/sec)
Downloading: https://my.remoterepository.com/nexus/content/repositories/snapshots/org/mainco/subco/first-module/87.0.0-SNAPSHOT/first-module-87.0.0-20151106.043202-8.pom
Downloaded: https://my.remoterepository.com/nexus/content/repositories/snapshots/org/mainco/subco/first-module/87.0.0-SNAPSHOT/first- module-87.0.0-20151106.043202-8.pom (3 KB at 21.9 KB/sec)
Downloading: http://download.java.net/maven/2/org/mainco/subco/subco/87.0.0-SNAPSHOT/maven-metadata.xml
Downloading: https://my.remoterepository.com/nexus/content/repositories/snapshots/org/mainco/subco/subco/87.0.0-SNAPSHOT/maven-metadata.xml

The dependency has a snapshot version. For snapshots, Maven will check the local repository and if the artifact found in the local repository is too old, it will attempt to find an updated one in the remote repositories. That is probably what you are seeing.
Note that this behavior is controlled by the updatePolicy directive in the repository configuration (which is daily by default for snapshot repositories).

Use mvn --help and you can see the options list.
There is an option like -nsu,--no-snapshot-updates Suppress SNAPSHOT updates
So use command mvn install -nsu can force compile with local repository.

To truly force maven to only use your local repo, you can run with mvn <goals> -o. The -o tells maven to let you work "offline", and it will stay off the network.

Follow below steps:
Ensure to delete all the contents of the jar folder located in your local except the jar that you want to keep.
For example files like .repositories, .pom, .sha1, .lastUpdated etc.
Execute mvn clean install -o command
This will help to use local repository jar files rather than connecting to any repository.

Maven always checks your local repository first, however,your dependency needs to be installed in your repo for maven to find it.
Run mvn install in your dependency module first, and then build your dependent module.

In my case I had a multi module project just like you. I had to change a group Id of one of the external libraries my project was depending on as shown below.
From:
<dependencyManagement>
<dependency>
<groupId>org.thirdparty</groupId>
<artifactId>calculation-api</artifactId>
<version>2.0</version>
<type>jar</type>
<scope>provided</scope>
</dependency>
<dependencyManagement>
To:
<dependencyManagement>
<dependency>
<groupId>org.thirdparty.module</groupId>
<artifactId>calculation-api</artifactId>
<version>2.0</version>
<type>jar</type>
<scope>provided</scope>
</dependency>
<dependencyManagement>
Pay attention to the <groupId> section. It turned out that I was forgetting to modifiy the corresponding section of the submodules that define this dependency in their pom files.
It drove me very crazy because the module was available locally.

Even when considering all answers above you might still run into issues that will terminate your maven offline build with an error. Especially, you may experience a warning as follwos:
[WARNING] The POM for org.apache.maven.plugins:maven-resources-plugin:jar:2.6 is missing, no dependency information available
The warning will be immediately followed by further errors and maven will terminate.
For us the safest way to build offline with a maven offline cache created following the hints above is to use following maven offline parameters:
mvn -o -llr -Dmaven.repo.local=<path_to_your_offline_cache> ...
Especially, option -llr prevents you from having to tune your local cache as proposed in answer #4.
Also take care that parameter <localRepositoryin> in your settings.xml points to the correct folder of your local Maven repository.

The -o option didn't work for me because the artifact is still in development and not yet uploaded and maven (3.5.x) still tries to download it from the remote repository because it's the first time, according to the error I get.
However this fixed it for me: https://maven.apache.org/general.html#importing-jars
After this manual install there's no need to use the offline option either.
UPDATE
I've just rebuilt the dependency and I had to re-import it: the regular mvn clean install was not sufficient for me

I had the exact same problem. Running mvn clean install instead of mvn clean compile resolved it.
The difference only occurs when using multi-maven-project since the project dependencies are uploaded to the local repository by using install.

use <classpathentry kind="var" path="M2_REPO/your jar location
without creating any environment veriable

just to give my 2 cents. For me, it was only necessary to find the jar inside the ~/.m2/repository directory and use its version. So, after using
cd local-dependency/
mvn install
the local-dependency jar will be in the .m2/repository/com/your-organization/local-dependency/
~$ tree ~/.m2/repository/com/your-organization/local-dependency/
/home/felipe/.m2/repository/com/your-organization/local-dependency/
├── 0.1-SNAPSHOT
│   ├── maven-metadata-local.xml
│   ├── _remote.repositories
│   ├── local-dependency-0.1-SNAPSHOT.jar
│   ├── local-dependency-0.1-SNAPSHOT.pom
│   └── resolver-status.properties
└── maven-metadata-local.xml
then use it as a local dependency
<dependency>
<groupId>com.your-organization</groupId>
<artifactId>local-dependency</artifactId>
<version>0.1-SNAPSHOT</version>
</dependency>

Related

Unable to build specific child projects in Maven using mvn package or IntelliJ tool windows

I have the following project structure in Maven:
./
├─ app1/
│ ├─ pom.xml
├─ app2/
│ ├─ pom.xml
├─ core/
│ ├─ pom.xml
├─ pom.xml
My parent pom.xml contains the following:
<packaging>pom</packaging>
<modules>
<module>core</module>
<module>app1</module>
<module>app2</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.9</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.acme</groupId>
<artifactId>parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
<repositories>
<repository>
<id>Maven central</id>
<url>https://repo1.maven.org/maven2/</url>
</repository>
<repository>
<id>Confluent</id>
<url>https://packages.confluent.io/maven/</url>
</repository>
<repository>
<id>Local Maven</id>
<name>Local Maven Repository</name>
<url>file://${user.home}/.m2/repository/</url>
</repository>
</repositories>
My core pom.xml contains the following:
<parent>
<groupId>com.acme</groupId>
<artifactId>parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<artifactId>core</artifactId>
<version>1.0.0-SNAPSHOT</version>
My app1 pom.xml contains the following:
<parent>
<groupId>com.acme</groupId>
<artifactId>parent</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>app1</artifactId>
<dependencies>
<dependency>
<groupId>com.acme</groupId>
<artifactId>core</artifactId>
<version>1.0.0-SNAPSHOT</version>
</dependency>
<!-- ... -->
</dependencies>
If I try to build in the root project directory, via:
mvn package
it correctly builds all my modules.
If I try to build the core package (which does not have additional intra-project dependencies) via mvn package, it correctly builds.
But if I try to build the app1 package (which does contain a dependency on my core module), even while forcing updates:
user#dev:~/parent/app1$ mvn package -U
[INFO] Scanning for projects...
[INFO]
[INFO] ---------------< com.acme:app1 >----------------
[INFO] Building app1 1.0.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
Downloading from Local Maven: file:///home/user/.m2/repository/com/acme/core/1.0.0-SNAPSHOT/maven-metadata.xml
Downloading from Maven central: https://repo1.maven.org/maven2/com/acme/core/1.0.0-SNAPSHOT/maven-metadata.xml
Downloading from Confluent: https://packages.confluent.io/maven/com/acme/core/1.0.0-SNAPSHOT/maven-metadata.xml
Downloading from Confluent: https://packages.confluent.io/maven/com/acme/parent/1.0.0-SNAPSHOT/maven-metadata.xml
Downloading from Maven central: https://repo1.maven.org/maven2/com/acme/parent/1.0.0-SNAPSHOT/maven-metadata.xml
Downloading from Local Maven: file:///home/user/.m2/repository/com/acme/parent/1.0.0-SNAPSHOT/maven-metadata.xml
Downloading from Maven central: https://repo1.maven.org/maven2/com/acme/parent/1.0.0-SNAPSHOT/event-processor-1.0.0-SNAPSHOT.pom
Downloading from Confluent: https://packages.confluent.io/maven/com/acme/parent/1.0.0-SNAPSHOT/event-processor-1.0.0-SNAPSHOT.pom
Downloading from Local Maven: file:///home/user/.m2/repository/com/acme/parent/1.0.0-SNAPSHOT/event-processor-1.0.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.324 s
[INFO] Finished at: 2023-01-06T11:54:58+01:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal on project app1: Could not resolve
dependencies for project com.acme:app1:jar:1.0.0-SNAPSHOT: Failed to
collect dependencies at com.acme:core:jar:1.0.0-SNAPSHOT: Failed to
read artifact descriptor for com.acme:core:jar:1.0.0-SNAPSHOT: Could
not find artifact com.acme:parent:pom:1.0.0-SNAPSHOT in Maven central
(https://repo1.maven.org/maven2/) -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException
I've read various docs (https://maven.apache.org/guides/introduction/introduction-to-the-pom.html) and some other questions and answers which seem a little dated.
These all state that if I run something like:
# In root project dir
mvn --projects app1 package --also-make
it correctly builds my single module and also any local dependencies, and it does.
However, I want to be able to use my IDE's (IntelliJ, in this case) Maven support and simply be able to run the module's Maven lifecycle task package from my IDE's Maven tool integration window.
Alternatively, at the very least, I'd like to just be able to go into the child module directory and do a mvn package and have it work. I expect that I can have a team member check out the repository and build a single module that they're working on instead of having to build everything (which may take a long time, especially if a core dependency or parent is changed).
How can I do this? Do I have to build from the parent context (i.e. from the root project dir)? If so, why doesn't Maven just tell me that instead of complaining that it can't find a pom.xml that is quite obviously there (and it knows where it is!)?
I know that dependency resolution is a hard problem, but I'm quite annoyed that such a common functionality such as building a child module is seemingly super hard to do with Maven...
All dependent packages need to be deployed in the local or distant repo before being resolved as dependencies in any of your module.
To deploy a module on which you depend (e.g core module) in the local repo use the mvn install command.
Then the mvn packages command should work in the app1 module.
Furthermore in your case you used
<relativePath/> <!-- lookup parent from repository -->
Which means that to be able to resolve the parent pom from any child module the parent-pom shall also be deployed in a local or distant repository. In your case you also have to mvn install the parent pom.
mvn package does not make your module "visible" to others, it just package everything by default in the target folder of the module. To make it visible locally use mvn install.
Finally for your team to be able to work as you wish you must deploy your jars including the parent pom in a remote / shared jar repository like Nexus for instance.
For that you use the mvn deploy command. But naturally, you need to set up all the deploy configuration and server beforehand... Then it would work as you expect

How to force Maven to use a specific repository (and only that)?

I am setting up a local Nexus repository and am trying to convince my local Maven setup to access that repo as the one and only. That Nexus repo has a group maven-central defined that (quoting from the Nexus manual:) "...accesses the Central Repository, formerly known as Maven Central." and is also supposed to cache the downloaded data.
I thus entered my local Nexus repo into my [home]\.m2\settings.xml like this:
...
<profile>
<id>alwaysActiveProfile</id>
<repositories>
<repository>
<id>RepoSynoProxy</id>
<name>Nexus Repo on Synology NAS</name>
<url>http://192.168.1.15:8081/repository/maven-central/</url>
</repository>
</repositories>
</profile>
</profiles>
...
<activeProfiles>
<activeProfile>alwaysActiveProfile</activeProfile>
</activeProfiles>
...
But for some strange reason, when I run Maven it starts downloading everything from https://repo.maven.apache.org/maven2/ instead of from the local URL, as I would have expected:
...
[INFO] Downloading from : http://192.168.1.15:8081/repository/maven-central/junit/junit/3.8.1/junit-3.8.1.pom
[INFO] Downloading from : https://repo.maven.apache.org/maven2/junit/junit/3.8.1/junit-3.8.1.pom
[INFO] Downloaded from : https://repo.maven.apache.org/maven2/junit/junit/3.8.1/junit-3.8.1.pom (998 B at 7.3 kB/s)
[INFO] Downloading from : http://192.168.1.15:8081/repository/maven-central/junit/junit/3.8.1/junit-3.8.1.jar
[INFO] Downloading from : https://repo.maven.apache.org/maven2/junit/junit/3.8.1/junit-3.8.1.jar
[INFO] Downloaded from : https://repo.maven.apache.org/maven2/junit/junit/3.8.1/junit-3.8.1.jar (121 kB at 1.4 MB/s)
...
My questions thus:
First: any idea anyone, why the requests to http://192.168.1.15:8081/repository/maven-central/... obviously don't work? I am using a freshly downloaded and installed Nexus v3.18.0 here. The Maven-central proxy is already predefined and I have nothing special configured. Apparently that doesn't work "out-of-the-box".
Second: where does Maven have the URL https://repo.maven.apache.org/maven2/... from that it is using as alternative? Is that hardcoded into Maven as fall-back if a repo doesn't work? Can one suppress that so that it ONLY accesses the repo defined in settings.xml?
You need to configure a mirror in the settings.xml that will override the repository definitions in the superpom and in other pom files: https://help.sonatype.com/display/NXRM3/Maven+Repositories#MavenRepositories-ConfiguringApacheMaven

Add local external jar - should not point at files within the project directory

In my maven project I use custom external my-custom-external jar like this:
pom.xml
<dependencies>
<dependency>
<groupId>com.myproject</groupId>
<artifactId>my-custom-external</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>system</scope>
<systemPath>${project.basedir}/libs/my-custom-external-1.0-SNAPSHOT.jar</systemPath>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>${kotlin.version}</version>
</dependency>
I success build project my mvn verify but in console I has warning:
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for com.myproject:prj_1:jar:1.0-SNAPSHOT
[WARNING] 'dependencies.dependency.systemPath' for com.myproject:my-custom-extneral:jar should not point at files within the project directory, ${project.basedir}/libs/my-custom-extneral-1.0-SNAPSHOT.jar will be unresolvable by dependent projects # line 62, column 25
[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]
you can use ${pom.basedir} to replace ${project.basedir}
It is just a warning and you are okay to do this as long as it is used only in this project as it won't be visible for the dependent projects.
You can consider below two options for doing it right
Install the 3rd party jar to your local repository and reference it just like any other dependency. Refer to this link for more details on installing the library
If you want to refer it from your project, then add your project libs folder as a repository as shown below
<repositories>
<repository>
<id>localrepository</id>
<url>file://${project.basedir}/libs</url>
</repository>
.
.
.
.
</repositories>
Hope this helps
I reached here because I faced a similar problem. And based on this answer by Prasann, I think I have a viable solution for this that will resolve most issues. Please feel free to correct me if I'm wrong.
Credit for the CheckSum related steps : Casey Jordan
During development, install the local external jar.
To do this, run the following command
mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=jar -DcreateChecksum=true
Fields inside < and > (including the < and >) need to be replaced with actual values.
Rename the maven-metadata-local files that are generated to remove the local part.
Copy the entire folder for this jar file (I'll call this as installed folder) from your local Maven repository to your project's lib folder. You need to maintain the folder structure from the root of the Maven repository.
For example, if the absolute path for your installed folder is C:\Users\so_user\.m2\repository\my_group_id\my_artifact_id\, then your project lib folder should also look like ${project.basedir}\lib\my_group_id\my_artifact_id\.
Also note that,
my_group_id in this point is the same as <group-id> in point #2. Just replace any periods (.) with a slash (\).
my_artifact_id in this point is the same as <artifact-id> in point #2. No changes required for this.
In your pom.xml file, create a local repository by adding the following lines:
<repositories>
<repository>
<id>any_name</id>
<name>Any Name</name>
<url>file://[path-to-lib-folder]</url>
</repository>
</repositories>
You can choose any name for id and name.
Replace [path-to-lib-folder] with the actual folder path. You can use ${project.basedir} or ${pom.basedir} here. No warning will pop up.
Add a dependency to the external jar as normal.
<dependencies>
<dependency>
<groupId>[group-id]</groupId>
<artifactId>[artifact-id]</artifactId>
<version>[version]</version>
</dependency>
</dependencies>
Replace [group-id], [artifact-id] and [version] with the actual values from point #1.
Because you have provided the entire installed folder in your project lib folder, you do not need to run mvn install:install-file again when you transfer this project to a new system.

Maven build on AWS wrongly using SNAPSHOT private repo

We have a private maven repo (artifactory) and we have a release jar (cryptolib-0.9.7.jar) in the libs-release folder.
Building locally or on Jenkins (hosted on Azure), all works fine. Building on codebuild on AWS, maven tries (and fails) to download from libs-snapshot instead.
Jenkins & local output (v3.3.9)...
Downloading: http://[our-repo]/artifactory/libs-release/org/openpseudonymiser/cryptolib/0.9.7/cryptolib-0.9.7.pom
Downloaded: http://[our-repo]/artifactory/libs-release/org/openpseudonymiser/cryptolib/0.9.7/cryptolib-0.9.7.pom (457 B at 3.7 KB/sec)
Downloading: http://[our-repo]/artifactory/libs-release/org/openpseudonymiser/cryptolib/0.9.7/cryptolib-0.9.7.jar
Downloaded: http://[our-repo]/artifactory/libs-release/org/openpseudonymiser/cryptolib/0.9.7/cryptolib-0.9.7.jar (13 KB at 234.8 KB/sec)
AWS/CodeBuild output (v.3.3.3)...
Downloading: http://[our-repo]/artifactory/libs-snapshot/org/openpseudonymiser/cryptolib/0.9.7/cryptolib-0.9.7.jar
Both are executing mvn -B install. I've tried all sorts of online suggestions and the the pom repository section currently looks like this (which now seems to ONLY attempt download from central)...
<repositories>
<repository>
<releases><enabled>false</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
<id>snapshots</id>
<name>libs-snapshot</name>
<url>http://[our-repo]/artifactory/libs-snapshot</url>
</repository>
<repository>
<releases><enabled>true</enabled></releases>
<snapshots><enabled>false</enabled></snapshots>
<id>central</id>
<name>libs-release</name>
<url>http://[our-repo]/artifactory/libs-release</url>
</repository>
</repositories>
What am I missing (apart from hair!!!)??
Doh! "When you use Maven with an AWS CodeBuild provided Java build environment, Maven pulls build and plugin dependencies from the secure central Maven repository at repo1.maven.org/maven2. This happens even if your build project's pom.xml file explicitly declares other locations to use instead." http://docs.aws.amazon.com/codebuild/latest/userguide/troubleshooting.html#troubleshooting-maven-repos
My solution was to delete the default settings.xml in the buildspec install phase.
phases:
install:
commands:
- rm /root/.m2/settings.xml

Hosting a Maven repository on github

I have a fork of a small open sourced library that I'm working on github. I'd like to make it available to other developers via maven, but I don't want to run my own Nexus server, and because it's a fork I can't easily deploy it to oss.sonatype.org.
What I'd like to do is to deploy it to github so that others can access it using maven. What's the best way to do this?
The best solution I've been able to find consists of these steps:
Create a branch called mvn-repo to host your maven artifacts.
Use the github site-maven-plugin to push your artifacts to github.
Configure maven to use your remote mvn-repo as a maven repository.
There are several benefits to using this approach:
Maven artifacts are kept separate from your source in a separate branch called mvn-repo, much like github pages are kept in a separate branch called gh-pages (if you use github pages)
Unlike some other proposed solutions, it doesn't conflict with your gh-pages if you're using them.
Ties in naturally with the deploy target so there are no new maven commands to learn. Just use mvn deploy as you normally would
The typical way you deploy artifacts to a remote maven repo is to use mvn deploy, so let's patch into that mechanism for this solution.
First, tell maven to deploy artifacts to a temporary staging location inside your target directory. Add this to your pom.xml:
<distributionManagement>
<repository>
<id>internal.repo</id>
<name>Temporary Staging Repository</name>
<url>file://${project.build.directory}/mvn-repo</url>
</repository>
</distributionManagement>
<plugins>
<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>
</plugins>
Now try running mvn clean deploy. You'll see that it deployed your maven repository to target/mvn-repo. The next step is to get it to upload that directory to GitHub.
Add your authentication information to ~/.m2/settings.xml so that the github site-maven-plugin can push to GitHub:
<!-- NOTE: MAKE SURE THAT settings.xml IS NOT WORLD READABLE! -->
<settings>
<servers>
<server>
<id>github</id>
<username>YOUR-USERNAME</username>
<password>YOUR-PASSWORD</password>
</server>
</servers>
</settings>
(As noted, please make sure to chmod 700 settings.xml to ensure no one can read your password in the file. If someone knows how to make site-maven-plugin prompt for a password instead of requiring it in a config file, let me know.)
Then tell the GitHub site-maven-plugin about the new server you just configured by adding the following to your pom:
<properties>
<!-- github server corresponds to entry in ~/.m2/settings.xml -->
<github.global.server>github</github.global.server>
</properties>
Finally, configure the site-maven-plugin to upload from your temporary staging repo to your mvn-repo branch on Github:
<build>
<plugins>
<plugin>
<groupId>com.github.github</groupId>
<artifactId>site-maven-plugin</artifactId>
<version>0.11</version>
<configuration>
<message>Maven artifacts for ${project.version}</message> <!-- git commit message -->
<noJekyll>true</noJekyll> <!-- disable webpage processing -->
<outputDirectory>${project.build.directory}/mvn-repo</outputDirectory> <!-- matches distribution management repository url above -->
<branch>refs/heads/mvn-repo</branch> <!-- remote branch name -->
<includes><include>**/*</include></includes>
<repositoryName>YOUR-REPOSITORY-NAME</repositoryName> <!-- github repo name -->
<repositoryOwner>YOUR-GITHUB-USERNAME</repositoryOwner> <!-- github username -->
</configuration>
<executions>
<!-- run site-maven-plugin's 'site' target as part of the build's normal 'deploy' phase -->
<execution>
<goals>
<goal>site</goal>
</goals>
<phase>deploy</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
The mvn-repo branch does not need to exist, it will be created for you.
Now run mvn clean deploy again. You should see maven-deploy-plugin "upload" the files to your local staging repository in the target directory, then site-maven-plugin committing those files and pushing them to the server.
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building DaoCore 1.3-SNAPSHOT
[INFO] ------------------------------------------------------------------------
...
[INFO] --- maven-deploy-plugin:2.5:deploy (default-deploy) # greendao ---
Uploaded: file:///Users/mike/Projects/greendao-emmby/DaoCore/target/mvn-repo/com/greendao-orm/greendao/1.3-SNAPSHOT/greendao-1.3-20121223.182256-3.jar (77 KB at 2936.9 KB/sec)
Uploaded: file:///Users/mike/Projects/greendao-emmby/DaoCore/target/mvn-repo/com/greendao-orm/greendao/1.3-SNAPSHOT/greendao-1.3-20121223.182256-3.pom (3 KB at 1402.3 KB/sec)
Uploaded: file:///Users/mike/Projects/greendao-emmby/DaoCore/target/mvn-repo/com/greendao-orm/greendao/1.3-SNAPSHOT/maven-metadata.xml (768 B at 150.0 KB/sec)
Uploaded: file:///Users/mike/Projects/greendao-emmby/DaoCore/target/mvn-repo/com/greendao-orm/greendao/maven-metadata.xml (282 B at 91.8 KB/sec)
[INFO]
[INFO] --- site-maven-plugin:0.7:site (default) # greendao ---
[INFO] Creating 24 blobs
[INFO] Creating tree with 25 blob entries
[INFO] Creating commit with SHA-1: 0b8444e487a8acf9caabe7ec18a4e9cff4964809
[INFO] Updating reference refs/heads/mvn-repo from ab7afb9a228bf33d9e04db39d178f96a7a225593 to 0b8444e487a8acf9caabe7ec18a4e9cff4964809
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 8.595s
[INFO] Finished at: Sun Dec 23 11:23:03 MST 2012
[INFO] Final Memory: 9M/81M
[INFO] ------------------------------------------------------------------------
Visit github.com in your browser, select the mvn-repo branch, and verify that all your binaries are now there.
Congratulations!
You can now deploy your maven artifacts to a poor man's public repo simply by running mvn clean deploy.
There's one more step you'll want to take, which is to configure any poms that depend on your pom to know where your repository is. Add the following snippet to any project's pom that depends on your project:
<repositories>
<repository>
<id>YOUR-PROJECT-NAME-mvn-repo</id>
<url>https://github.com/YOUR-USERNAME/YOUR-PROJECT-NAME/raw/mvn-repo/</url>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
</repositories>
Now any project that requires your jar files will automatically download them from your github maven repository.
Edit: to avoid the problem mentioned in the comments ('Error creating commit: Invalid request. For 'properties/name', nil is not a string.'), make sure you state a name in your profile on github.
Don't use GitHub as a Maven Repository.
Edit: This option gets a lot of down votes, but no comments as to why. This is the correct option regardless of the technical capabilities to actually host on GitHub. Hosting on GitHub is wrong for all the reasons outlined below and without comments I can't improve the answer to clarify your issues.
Best Option - Collaborate with the Original Project
The best option is to convince the original project to include your changes and stick with the original.
Alternative - Maintain your own Fork
Since you have forked an open source library, and your fork is also open source, you can upload your fork to Maven Central (read Guide to uploading artifacts to the Central Repository) by giving it a new groupId and maybe a new artifactId.
Only consider this option if you are willing to maintain this fork until the changes are incorporated into the original project and then you should abandon this one.
Really consider hard whether a fork is the right option. Read the myriad Google results for 'why not to fork'
Reasoning
Bloating your repository with jars increases download size for no benefit
A jar is an output of your project, it can be regenerated at any time from its inputs, and your GitHub repo should contain only inputs.
Don't believe me? Then check Google results for 'dont store binaries in git'.
GitHub's help Working with large files will tell you the same thing. Admittedly jar's aren't large but they are larger than the source code and once a jar has been created by a release they have no reason to be versioned - that is what a new release is for.
Defining multiple repos in your pom.xml slows your build down by Number of Repositories times Number of Artifacts
Stephen Connolly says:
If anyone adds your repo they impact their build performance
as they now have another repo to check artifacts against... It's not a big
problem if you only have to add one repo... But the problem grows and the next
thing you know your maven build is checking 50 repos for every artifact and
build time is a dog.
That's right! Maven needs to check every artifact (and its dependencies) defined in your pom.xml against every Repository you have defined, as a newer version might be available in any of those repositories.
Try it out for yourself and you will feel the pain of a slow build.
The best place for artifacts is in Maven Central, as its the central place for jars, and this means your build will only ever check one place.
You can read some more about repositories at Maven's documentation on Introduction to Repositories
You can use JitPack (free for public Git repositories) to expose your GitHub repository as a Maven artifact. Its very easy. Your users would need to add this to their pom.xml:
Add repository:
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
Add dependency:
<dependency>
<groupId>com.github.User</groupId>
<artifactId>Repo name</artifactId>
<version>Release tag</version>
</dependency>
As answered elsewhere the idea is that JitPack will build your GitHub repo and will serve the jars. The requirement is that you have a build file and a GitHub release.
The nice thing is that you don't have to handle deployment and uploads. Since you didn't want to maintain your own artifact repository its a good match for your needs.
Since 2019 you can now use the new functionality called Github package registry.
Basically the process is:
generate a new personal access token from the github settings
add repository and token info in your settings.xml
deploy using
mvn deploy -Dregistry=https://maven.pkg.github.com/yourusername -Dtoken=yor_token
Another alternative is to use any web hosting with webdav support. You will need some space for this somewhere of course but it is straightforward to set up and a good alternative to running a full blown nexus server.
add this to your build section
<extensions>
<extension>
<artifactId>wagon-webdav-jackrabbit</artifactId>
<groupId>org.apache.maven.wagon</groupId>
<version>2.2</version>
</extension>
</extensions>
Add something like this to your distributionManagement section
<repository>
<id>release.repo</id>
<url>dav:http://repo.jillesvangurp.com/releases/</url>
</repository>
Finally make sure to setup the repository access in your settings.xml
add this to your servers section
<server>
<id>release.repo</id>
<username>xxxx</username>
<password>xxxx</password>
</server>
and a definition to your repositories section
<repository>
<id>release.repo</id>
<url>http://repo.jillesvangurp.com/releases</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
Finally, if you have any standard php hosting, you can use something like sabredav to add webdav capabilities.
Advantages: you have your own maven repository
Downsides: you don't have any of the management capabilities in nexus; you need some webdav setup somewhere
As an alternative, Bintray provides free hosting of maven repositories. That's probably a good alternative to Sonatype OSS and Maven Central if you absolutely don't want to rename the groupId. But please, at least make an effort to get your changes integrated upstream or rename and publish to Central. It makes it much easier for others to use your fork.
If you have only aar or jar file itself, or just don't want to use plugins - I've created a simple shell script. You can achieve the same with it - publishing your artifacts to Github and use it as public Maven repo.
I'd like to add another alternative, a Gradle plugin I've been working on lately: magik.
Basically it allows to publish directly on a github repository acting as a maven repository.
I came here looking to do the same thing, unlitmately host my Maven repository for free, but after more research I ended up here:
https://jfrog.com/start-free/
The setup was quite strightforward, has a good free tier which will serve me for the forseeable future, and has additional (paid for) upgrades which may well come in handy in the future.
So far I am very pleased indeed !

Resources