I'm using the following command to create a local repository within my project (used on many systems so I was experimenting a bit with having a local maven repository inside my project folder for storing external jars that can't be found on any online repository):
C:\Users\someone\workspace\someProject>mvn install:install-file
-Dfile=somePackage-1.0.0.jar
-DgroupId=foo.bar
-DartifactId=somePackage
-Dversion=1.0.0
-Dpackaging=jar
-DlocalRepositoryPath=libs
-DcreateChecksum=true
This is run from Windows command line. Somehow, even with -DlocalRepositoryPath, it's still installing the jar into the default local repository (C:\Users\someone\.m2\, etc.). What am I doing wrong here? I tried different variations of the libs path, like /libs, /libs/, full path, using "", but nothing worked. Why isn't the -DlocalRepositoryPath argument not working here? I'm using Maven 2.2.1.
The install:install-file option ignores the localRepositoryPath when using the version 2.2 of the plugin. However, it works with version 2.3 and higher.
Also, try using the fully qualified name of the plugin to specify the version:
mvn org.apache.maven.plugins:maven-install-plugin:2.3.1:install-file \
-Dfile=<path-to-your-file> -DgroupId=<myGroup> \
-DartifactId=<myArtifactId> -Dversion=<myVersion> \
-Dpackaging=<myPackaging> -DlocalRepositoryPath=<path>
Related
I have a scenario, where in my maven repository, the required JAR is available, but it is not inside the version folder, instead, it is directly under the group.
For Example I need test-1.0.0.jar
In my Maven Repo, the jar is placed in the path like below,
com.java.test
----test-1.0.0.jar
But it is supposed to be like the below,
com.java.test
---1.0.0
------test-1.0.0.jar
Because of this, the jar is not downloading when I do maven install. Are there any workarounds to get the jar downloaded without changing the maven repository structure?
I think there is a problem with pom.xml of test.jar or jar uploaded to the remote repo incorrectly.
In that case, if you have control over test.jar codebase or remote repo, you can figure out what is wrong and fix it. If you don't have control over them, you can treat like it is 3rd party jar. Using below command you can populate the jar into your local maven repository.
mvn install:install-file
Basically, this command reads this dependency and installs into your local maven repository within the constraints you provided as a parameter.
Below example have been taken from Apache Maven Documentation.
mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>
But keep in mind, it is just a workaround for your local development. In the long run, the actual problem needs to be resolved. As mentioned earlier, either pom.xml of test.jar should be fixed or structure of remote repository should be corrected by re-uploading the jar.
You would need to re-upload your jar to the right path in Nexus, using mvn deploy:deploy-file :
In Windows:
cmd /v /c "set g=com.java&& set a=test&& set v=1.0.0&& mvn deploy:deploy-file -Dfile=!a!-!v!.jar -Dpackaging=jar -DgroupId=!g! -DartifactId=!a! -Dversion=!v! -DrepositoryId=your-nexus-id -Durl=https://nexus.your.comany.com repository/public"
You can execute that in the folder where the jar is, even without any pom.xml.
When I created a Spring Boot application I could see mvnw and mvnw.cmd files in the root of the project. What is the purpose of these two files?
These files are from Maven wrapper. It works similarly to the Gradle wrapper.
This allows you to run the Maven project without having Maven installed and present on the path. It downloads the correct Maven version if it's not found (as far as I know by default in your user home directory).
The mvnw file is for Linux (bash) and the mvnw.cmd is for the Windows environment.
To create or update all necessary Maven Wrapper files execute the following command:
mvn -N io.takari:maven:wrapper
To use a different version of maven you can specify the version as follows:
mvn -N io.takari:maven:wrapper -Dmaven=3.3.3
Both commands require maven on PATH (add the path to maven bin to Path on System Variables) if you already have mvnw in your project you can use ./mvnw instead of mvn in the commands.
Command mvnw uses Maven that is by default downloaded to ~/.m2/wrapper on the first use.
URL with Maven is specified in each project at .mvn/wrapper/maven-wrapper.properties:
distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.3.9/apache-maven-3.3.9-bin.zip
To update or change Maven version invoke the following (remember about --non-recursive for multi-module projects):
./mvnw io.takari:maven:wrapper -Dmaven=3.3.9
or just modify .mvn/wrapper/maven-wrapper.properties manually.
To generate wrapper from scratch using Maven (you need to have it already in PATH run:
mvn io.takari:maven:wrapper -Dmaven=3.3.9
The Maven Wrapper is an excellent choice for projects that need a specific version of Maven (or for users that don't want to install Maven at all). Instead of installing many versions of it in the operating system, we can just use the project-specific wrapper script.
mvnw: it's an executable Unix shell script used in place of a fully installed Maven
mvnw.cmd: it's for Windows environment
Use Cases
The wrapper should work with different operating systems such as:
Linux
OSX
Windows
Solaris
After that, we can run our goals like this for the Unix system:
./mvnw clean install
And the following command for Batch:
./mvnw.cmd clean install
If we don't have the specified Maven in the wrapper properties, it'll be downloaded and installed in the folder $USER_HOME/.m2/wrapper/dists of the system.
Maven Wrapper plugin
Maven Wrapper plugin to make auto installation in a simple Spring Boot project.
First, we need to go in the main folder of the project and run this command:
mvn -N io.takari:maven:wrapper
We can also specify the version of Maven:
mvn -N io.takari:maven:wrapper -Dmaven=3.5.2
The option -N means –non-recursive so that the wrapper will only be applied to the main project of the current directory, not in any submodules.
Source 1 (further reading): https://www.baeldung.com/maven-wrapper
short answer: to run Maven and Gradle in the terminal without following manual installation processes.
Gradle example:
./gradlew clean build
./gradlew bootRun
Maven example:
./mvnw clean install
./mvnw spring-boot:run
"The recommended way to execute any Gradle build is with the help of the Gradle Wrapper (in short just “Wrapper”). The Wrapper is a script that invokes a declared version of Gradle, downloading it beforehand if necessary. As a result, developers can get up and running with a Gradle project quickly without having to follow manual installation processes saving your company time and money."
Gradle would also add some specific files corresponding to the Maven files Gradlew and Gradle.bat
In the windows OS, mvnw clean install is used for the maven clean and install activity, and mvnw spring-boot:run is used to run the Spring boot application from Command Prompt.
For Eaxmple:
C:\SamplesSpringBoot\demo>mvnw clean install
C:\SamplesSpringBoot\demo>mvnw spring-boot:run
By far the best option nowadays would be using a maven container as a builder tool. A mvn.sh script like this would be enough:
#!/bin/bash
docker run --rm -ti \
-v $(pwd):/opt/app \
-w /opt/app \
-e TERM=xterm \
-v $HOME/.m2:/root/.m2 \
maven mvn "$#"
If I know the coordinates of an artifact, and a name of the class inside that artifact, can I make Maven run the class, including all of its dependencies on the Java classpath?
For example, suppose a coworker told me about a tool I can run, which is published to our internal Nexus with the artifact coordinates example:cool-tools:1.0.0. I used this answer to download the artifact. Now, I know that the main class name is example.Main. But if I just go to the artifact's download location and run java -cp cool-tools-1.0.0.jar example.Main, I get NoClassDefFoundErrors for any dependencies of cool-tools.
I'm aware of the maven-exec-plugin, but as far as I can tell that's only for projects where you have the source. Suppose I don't have access to the source, only the Nexus containing the tool (and all its dependencies). Ideally, I'd do something like mvn exec:exec -DmainArtifact='example:cool-tools:1.0.0' -DmainClass='example.Main', but I don't think the exec plugin is actually capable of this.
ETA: To be clear, I do not have a local project / POM. I want to do this using only the command line, without writing a POM, if possible.
There is no out-of-the-box solution for your task. But you can create a simple script to solve it:
Download pom.xml of your tool from the repo.
Download jar of your tool.
Download all its dependencies.
Run java against resolved libraries.
Command line:
> mvn dependency:copy -Dartifact=<tool.group.id>:<tool.artifact.id>:<tool.version>:pom -DoutputDirectory=target
> mvn dependency:copy -Dartifact=<tool.group.id>:<tool.artifact.id>:<tool.version> -DoutputDirectory=target
> mvn dependency:copy-dependencies -f target/<tool.artifact.id>-<tool.version>.pom -DoutputDirectory=target
> java -cp target/* <tool.main.class>
Directory ./target will contain your tool + all dependencies.
See details on dependency:copy and dependency:copy-dependencies mojos.
Edit
As alternative, you can build classpath using libraries in the local repo by:
> mvn dependency:build-classpath -DincludeScope=runtime -f target/<tool.artifact.id>-<tool.version>.pom [-Dmdep.outputFile=/full/path/to/file]
See details on build-classpath mojo.
You could download the pom from the repository using wget, for instance. Then resolve the dependencies, and build the classpath exporting it to a file using Maven. Finally, execute with Java and the built classpath using something like bash backticks to use the content of the file.
Just like in this answer.
For me the first anwer almost worked, but I needed to slightly adjust the script. In the end I came (on windows machine) to following solution:
> mvn dependency:copy -Dartifact=<tool.group.id>:<tool.artifact.id>:<tool.version>:pom -DoutputDirectory=target
> mvn dependency:copy -Dartifact=<tool.group.id>:<tool.artifact.id>:<tool.version> -DoutputDirectory=target
> mvn dependency:copy-dependencies -f target/<tool.artifact.id>-<tool.version>.pom -DoutputDirectory=target
> cd target
> java -cp target/*;<tool.artifact.id>-<tool.version>.jar <tool.main.class>
On Unix/Linux machine in the last command the semicolon ";" must be replaced with colon ":".
When input arguments must be provided, just put them in the last script line:
> java -cp target/*;<tool.artifact.id>-<tool.version>.jar <tool.main.class> arg1 arg2 ...
u can use IDEs like Intellij idea which automatically resolve dependencies as u write them in your pom
As it has been mentioned by others already there is no solution without creating an extra POM.
One solution could be to use the Maven Shade Plugin in this POM: "This plugin provides the capability to package the artifact in an uber-jar, including its dependencies"
I think the Executable JAR is close to that what you'd like to achieve.
How does my firm get a jar into a maven repo so maven projects can access it from inhouse.
Can someone please point me to a good step by step details on how to do the following
Make a jar with Maven
Get the jar installed into a local maven repo
I doubt your company wants their private internal code hosted on a public repository:
Install your own repository server inside your own network, I use Archiva. This is the most ideal solution, then you can set up Mavenized projects to automatically upload themselves to your private repository when you do mvn:release and everyone will see the new versions. How to use Archiva is all very well documented.
If they have open source code that want to share, that is different:
You can publish public facing open source code through Sonatype.
If you just want to install a dependency to a local repository:
If you just want to install a .jar locally that is easy and well documented.
mvn install:install-file -Dfile=path-to-your-artifact-jar \
-DgroupId=your.groupId \
-DartifactId=your-artifactId \
-Dversion=version \
-Dpackaging=jar
I have jar files that cannot be found on Maven Central repository. I would like to add the jar so I can just include extra tag in my pom.xml file and other developer can use the jar. What are the steps needed to upload the jar to http webserver webfolder? What file should I uploaded beside custom.jar? What other files need to exist on the webfolder side by side with custom.jar?
If you already have a web server set up pointing on a web folder, a simple way to deploy your custom JAR would to use the deploy:deploy-file Mojo. As documented in the Usage page of the Maven Deploy Plugin:
The deploy:deploy-file mojo is used
primarily for deploying artifacts to
which were not built by Maven. The
project's development team may or may
not provide a POM for the artifact,
and in some cases you may want to
deploy the artifact to an internal
remote repository. The deploy-file
mojo provides functionality covering
all of these use cases, and offers a
wide range of configurability for
generating a POM on-the-fly.
Additionally, you can specify what
layout your repository uses. The full
usage statement of the deploy-file
mojo can be described as:
mvn deploy:deploy-file -Durl=file://C:\m2-repo \
-DrepositoryId=some.id \
-Dfile=your-artifact-1.0.jar \
[-DpomFile=your-pom.xml] \
[-DgroupId=org.some.group] \
[-DartifactId=your-artifact] \
[-Dversion=1.0] \
[-Dpackaging=jar] \
[-Dclassifier=test] \
[-DgeneratePom=true] \
[-DgeneratePom.description="My Project Description"] \
[-DrepositoryLayout=legacy] \
[-DuniqueVersion=false]
Only the 3 first parameters are mandatory (short version). If you wonder what the repositoryId is, the documentation of the Mojo says:
Server Id to map on the <id> under <server> section of settings.xml In most cases, this parameter will be required for authentication. Default value is: remote-repository.
In other words, the simplest way to use this would be to copy your custom JAR on the machine hosting the web server and to use the file:// protocol when specifying the URL. There is no additional setup required. If you want to deploy remotely, then scp:// is often the preferred protocol (there are others but this one is pretty easy to setup). Below, an example using scp:
mvn deploy:deploy-file -DgroupId=my.group -DartifactId=myartifact -Dversion=1.0 \
-DgeneratePom=true \
-Dpackaging=jar \
-Dfile=custom.jar \
-DrepositoryId=some.id \
-Durl=scp://REMOTEMACHINE/PATH/TO/WEB_ROOT/maven2_repository
Actually, using a web server to host your own Maven repository is perfectly fine but it can be a bit painful to initialize. One solution to solve this issue is to use a Maven proxy (like Nexus for example) instead of just a Maven repository. But this goes beyond your question.
For more resources on this, check (the principles are still valid even if the implementation solutions are a bit outdated):
Using Maven in a corporate environment
Creating the repositories
Nexus Book: Repository Management with Nexus
Preferably, you would need a local maven repository. One option for this is Nexus
Or if you are working just yourself, you can save the overhead and put the jars in the repository on your machine - under home/.m2/repository, in an appropriate folder
Next command helps to install the jar to the local repository. After this you can upload folder with the jar from local to the remote repository.
mvn install:install-file \
-DgroupId=com.name \
-DartifactId=aaaa-bc \
-Dversion=1.0 \
-Dpackaging=jar \
-Dfile=aaaa-bc.jar \
-DcreateChecksum=true