I would need to execute a specific Maven plugin from command line. For example, in the following I execute a specific version of Maven Surefire Plugin to test Java projects:
mvn org.apache.maven.plugins:maven-surefire-plugin:2.19-SNAPSHOT:test
However, the above assumes to find the surefire plugin 2.19 in the default Maven repository path. Now, my question is, if I want to use the plugin with a specific path (not Maven default one), what should I do using the command line? I would expect something like the following, without modifying pom.xml:
mvn /path/to/some/jar/version/org.apache.maven.plugins:maven-surefire-plugin:2.19-SNAPSHOT:test
or more generally, for the following invocation
mvn groupId:artifactId:version:goal
I would need somewhere to specify a customized path to execute its goal
mvn /some/path/to/groupId:artifactId:version:goal
On the other hand, please let me know if this is not even supported by Maven.
This is not how it works. Maven will always look-up artifacts inside your local repository. And if it can't find it in your local repository, it will try to download it from configured remote repositories.
As such, you don't specify a path to a plugin. You specify a path to a local repository, where that plugin is installed. In this local repository will be installed all the dependencies of the plugin you're trying to invoke. This also means that you cannot have a JAR to a plugin "sitting around" anywhere; it needs to be located inside a correct repository tree directory. With a local repository of /my/local/repo, the artifacts of the plugin groupId:artifactId:version must be located in /my/local/repo/groupId/artifactId/version and be named artifactId-version.jar (and .pom). In the same say, the location of the dependencies of that plugin must follow that directory structure.
By default, the local repository is located inside ~/.m2/repository. But you can change that by:
Specifying the maven.repo.local system property on the command line, for example with mvn -Dmaven.repo.local=/path/to/local/repo groupId:artifactId:version:goal;
Use a custom settings.xml and tell Maven to use it with the -s command line option. It would contain:
<settings>
<localRepository>/path/to/local/repo</localRepository>
</settings>
and be used with mvn -s /path/to/settings.xml groupId:artifactId:version:goal
Related
I have a script that shall use maven to download an artifact to the local maven repository:
mvn org.apache.maven.plugins:maven-dependency-plugin:3.1.2:get -Dartifact=org.jasypt:jasypt:1.9.3
However, when the script is executed from a directory that contains a pom.xml then maven also parses this POM and prints the reactor, etc. causing confusion. Even worse when the pom.xml is invalid for whatever reason the command fails.
What I want is to ignore any local pom.xml. Is there a way to do this with a maven CLI option or something similar?
I already studied the CLI options but could not find any option for this. I tried with something like -f /tmp but it seems that -f option does the oposite of what I want as it requires a POM file or a directory containing a pom.xml or the build will immediately fail.
Any other ideas instead of creating an empty directory in my script, CDing to it, invoking maven and then CD back and delete the empty directory?
My work-around is to have a minimal POM reflecting an artifact created by the Maven invocation that needs to ignore the actual POM,
groupId=".."
artId=".."
ver=".."
echo "<project>
<modelVersion>4.0.0</modelVersion>
<groupId>${groupId}</groupId>
<artifactId>${artId}</artifactId>
<version>${ver}</version>
</project>" > target/min-pom.xml
mvn -f target/min-pom.xml PLUGIN_GOAL_CREATING_GROUP_ARTIFACT_VER
https://maven.apache.org/guides/introduction/introduction-to-the-pom.html#minimal-pom
I've got one myPackage maven project, compiled and installed to local maven repository under
~/.m2/repository/mygroup/myPackage/1.0-SNAPSHOT/myPackage-1.0-SNAPSHOT.jar
In another maven project, I wish to use it, and in pom.xml I write <dependency> section for it. But I don't know how to write the "systemPath" for this jar:
I cannot use "~" to specify the path, because "~" is a *nix shell extention, java/maven cannot recognize it.
I cannot hard code like
/home/myself/.m2/...
It's not portable.
I cannot use ${project.basedir} because these 2 maven projects are under different folders. But I guess there should be some other maven environment variables that could indicate "home directory"?
All I wish to do is to get this "systemPath" done.
---------------Problem solved by using another project as dependency------------
<systemPath>${project.basedir}/../myPackage/pom.xml</systemPath>
That works!
A system path is required when the library that your project depends on is not in the maven local repository.
As a rule of thumb, this approach is indeed not portable at all and should be avoided for real projects.
Now, the dependency is in local repository if:
It was downloaded from some remote repository (usually)
You've installed it locally (in this case its in your local repository but might not be in your team-mate repo)
In order to install the dependency into the local repo consider using: mvn install:install-file+ parameters as written here
But from your question, it looks like the file is already there... Anyway once the file is in the local repository you can just define a "regular" dependency (group, artifact, version) and Maven will pick it, no need to fiddle with system Path in this case.
I have a Maven repo which contains all of the dependencies needed for a Java project.
How do I add the maven repo into the classpath environment variable so it can be access via the console?
Working with a project that has a bash script to setup the development environment. This also runs some Java code but is can not see any for the dependencies of the project.
You can configure either the setting.xml at user level (${user.home}/.m2/settings.xml) or the global level configuration (${maven.home}/conf/settings.xml) by specifying the localRepository element to define the local repository path. Setting the environment variable MY_M2_REPO, before running maven, will effectively define the local repository path.
<localRepository>${env.MY_M2_REPO}</localRepository>
Alternatively, you can use the argument maven.repo.local to specify the location of your local repository when running maven.
mvn clean package -Dmaven.repo.local=/home/user/maven_repo
I am trying to deploy signature files separately using deploy-file goal to Nexus staging repository, but I noticed that mvn deploy plugin removes the extension. My file is something like: azerty-0.1.jar.asc
but the file that gets deployed is: azerty-0.1.asc
I tried adding a classifier: -Dclassifier=jar
the file that gets deployed is: azerty-0.1-jar.asc
This seems like a strange behaviour.
Question: Any ideas how to work around it?
This is rather a normal behavior, Maven is using the file extension as artefact packaging, from maven-deploy-plugin, deploy-file, packaging option:
Type of the artifact to be deployed. Retrieved from the <packaging> element of the POM file if a POM file specified. Defaults to the file extension if it is not specified via command line or POM.
Note: bold is mine.
Moreover, the classifier option would indeed add an - between the version and the string provided as classifier: that's maven convention.
In your case you want to specify a special packaging, which would be jar.asc if you really want the remote file to have as extension jar.asc.
The following would hence work:
mvn deploy:deploy-file -Dfile=azerty-0.1.jar.asc -Dpackaging=jar.asc -DrepositoryId=your_id -Durl=http://your_repository -DgroupId=your_groupId -DartifactId=azerty -Dversion=0.1
Note the -Dpackaging=jar.asc which effectively tells Maven the file extension would be jar.asc.
As a general note, if you are using the repository as a build store, that would still be reasonable, otherwise in your case you would push to a Maven repository an artifact which would then be difficult (or rather weird) to import in a project.
If instead this is really an additional artifact of your project, you should look at the attach-artifact goal of the build-helper-maven-plugin, to effective define it as additional artifact, then Maven will automatically add it to its install and deploy phase.
I have an environment variable called $MYCLASSPATH that contains a set of directories that contain JARS. An example of my environment variable could be the following:
/project1/jars/:/project2/jars:/project3/jars
I also have a maven project that contains some external dependencies that are defined in the pom.xml. However, I want to include all the directories listed in the above environment variable in Maven since some JARS are required for the compilation.
Without Maven I could do this:
javac -cp "$MYCLASSPATH" path/to/my/java_file
How can I add all these directories to Maven?
As an addition to crowne's answer, once you know the coordinates the external dependencies would have to be added to your local maven repository using mvn install:install-file
For example
mvn install:install-file -Dfile=path_to_your_jar -DgroupId=X -DartifactId=X -Dversion=X -Dpackaging=jar
Your POM can then contain these dependencies in the same way normal dependencies are declared eg.
<dependency>
<groupId>X</groupId>
<artifactId>X</artifactId>
<version>X</version>
</dependency>
Each jar should be identified by a specific set of maven coordinates
https://maven.apache.org/pom.html#Maven_Coordinates
This lets us know which version of the jar the project is dependent on and where to get it from in the maven repository.
It also allows the project to be built correctly from another computer with a different set of environment variables.
So you should define your dependencies in your pom using the correct maven coordinates.