Excluding some dependency from mvn dependency:resolve - java

In my pom.xml of com.test:Service:1.0, I have a dependency on some local jar: com.test:Parser:1.0
I want to resolve all dependencies except for it, since I install it manually to my local maven. Resolve command:
mvn -B dependency:resolve -DincludeParents=true
But it fails on:
[ERROR] Failed to execute goal on project Service: Could not resolve
dependencies for project com.test:Service:jar:1.0: Could not find
artifact com.test:Parser:jar:1.0 in central
(https://repo.maven.apache.org/maven2)
Then I tried to add the options
-DexcludeGroupIds=com.test -DexcludeArtifactIds=Parser
but am still getting the same error. Am I misusing the options?
Reference: http://maven.apache.org/plugins/maven-dependency-plugin/resolve-mojo.html

I'm using 3.8.6 and it looks like go-offline works with exclude*, but resolve does not:
mvn dependency:go-offline -DexcludeGroupIds=com.test
According to: https://maven.apache.org/plugins/maven-dependency-plugin/index.html
dependency:go-offline tells Maven to resolve everything this project is dependent on (dependencies, plugins, reports) in preparation for going offline.
dependency:resolve tells Maven to resolve all dependencies and displays the version.

This seems to simply be a bug. See
https://issues.apache.org/jira/browse/MDEP-568
https://github.com/apache/maven-dependency-plugin/pull/2
The solution, as proposed in the above threads, is to use a different library: https://github.com/qaware/go-offline-maven-plugin
In your pom.xml add the plugin:
<plugin>
<groupId>de.qaware.maven</groupId>
<artifactId>go-offline-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<dynamicDependencies>
</dynamicDependencies>
</configuration>
</plugin>
Then use the command mvn de.qaware.maven:go-offline-maven-plugin:resolve-dependencies with the required options.

Related

How to find the dependencies of a maven plugin goal

I'm trying to find out where the dependencies are coming from for a particular plugin goal. I know the dependencies exist because Maven downloaded them when I ran the goal for the first time. And they're not (directly) dependencies of my project, because I ran mvn clean install first, and these dependencies weren't downloaded then.
In this specific case, I'm trying to figure out what the dependencies are when I run mvn sonar:sonar, but I expect the answer will be general purpose. For instance, even though I've built this project a number of times, when I ran that goal Maven downloaded a bunch of new jars like maven-antrun-plugin.
Here are things I've tried:
mvn dependency:tree shows the dependencies for the project, but not for the plugin goal (it doesn't include anything related to SonarQube in the list).
mvn org.apache.maven.plugins:maven-help-plugin:3.2.0:effective-pom -Dverbose=true also doesn't include anything related to SonarQube.
mvn -X sonar:sonar prints out what looks like a dependency graph, but it's missing the jars that Maven downloaded the first time I ran the sonar:sonar goal.
mvn -X dependency:resolve-plugins seems to be meant to download the dependencies of plugins, but does not capture the sonar:sonar dependencies. If I clear out my Maven cache, run mvn dependency:resolve-plugins, and then run mvn sonar:sonar, Maven has to download jars.
Use your IDE to navigate to the POM of the plugin project and then look at the dependency tree.
Or do this manually by copying the plugin POM to your file system and running mvn dependency:tree.
The dependencies downloaded by maven are generally stored locally on the pc at the address C:\Users\yourUser/.m2 this regardless of your projects and so that you do not have to reload dependencies that you are already using in other projects.
I hope I have understood your question and that my answer is useful to you, greetings.

Not able to do maven release with jasypt encryption

I've a Springboot project handled by maven that contains some secrets encrypted with Jasypt. When I'm running the mvn deploy I'm passing the jasypt password as:
mvn -B clean deploy -Djasypt.encryptor.password=${jasypt_password}
And it is able to run test cases and deploy the jar file to repository. But when I'm doing the same with mvn release the jasypt password is not properly set.
mvn -B clean release:prepare release:perform -Djasypt.encryptor.password=${jasypt_password}
Or
mvn -B release:prepare -Djasypt.encryptor.password=${jasypt_password}
For both these cases, I'm getting following error while running the test cases.
Caused by: java.lang.IllegalStateException: Required Encryption configuration property missing: jasypt.encryptor.password
The plugin configuration I'm using is:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-release-plugin</artifactId>
<version>2.5.3</version>
<configuration>
<localCheckout>true</localCheckout>
</configuration>
</plugin>
How should I set jasypt password while running the maven release?
I got the issue solved using following command. Apparently maven release plugin takes configuration parameters in a different way.
mvn -B clean release:prepare release:perform -Darguments="-Djasypt.encryptor.password=${jasypt_password}"
You can check this link which gives more details about the issue.
Basically it states that
Once you enable jasypt-spring-boot the password property is required
as specified in the documentation
They have suggested multiple solutions. One of them being:
Add jasypt.encryptor.password=dummy to your springboot properties file.
And another (if running from command line), run your jar with
-Djasypt.encryptor.password=*******************
If you are using Intellij,
Using
-Djasypt.encryptor.password=${jasypt_password} as Project argument should resolve your issue.
If it doesnot help then try
--jasypt.encryptor.password=${jasypt_password}.
Let me know if this helps.
Thanks,
Manu

Running maven plugins on artifact's pom file

Many of maven's plugins run on the current project's pom.xml file.
Sometimes I need to run a certain plugin on an artifact I downloaded from the repository.
For instance I'm downloading sparkjava using dependency:get like that:
mvn dependency:get -Dartifact com.sparkjava:spark-core:2.5.4
I would like then download all sparkjava's dependencies sources using dependency:sources like that:
mvn dependency:sources -Dartifact com.sparkjava:spark-core:2.5.4
Or even better, run dependency:sources directly on the artifact:
mvn dependency:sources -DinputPom=locationToRepository/com/sparkjava/2.5.4/spark-core-2.5.4.pom
Is it possible to do?
The maven dependency plugin sources goal tells Maven to resolve all dependencies and their source attachments, and displays the version.
You can specifically includes or excludes artifacts by using optional parameters. See the maven dependency plugin documentation.

Can I remove jar from local repository without pom.xml?

I install my jar to local repository by command:
mvn install:install-file -Dfile=aaa.jar -DgroupId=bbb -DartifactId=ccc -Dversion=1.0 -Dpackaging=jar
Now I want remove it from repository. I try command:
mvn dependency:purge-local-repository -DmanualInclude=bbb-ccc
But get error:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-dependency-plugin:2.8:purge-local-repository (default-cli): Goal requires a project to execute but there is no POM in this directory (...). Please verify you invoked Maven from the correct directory. -> [Help 1]
Then I create pom.xml with data:
<?xml version="1.0" encoding="UTF-8"?>
<project>
<groupId>bbb</groupId>
<artifactId>ccc</artifactId>
<version>1.0</version>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<properties>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
<packaging>${project.packaging}</packaging>
<file>aaa.jar</file>
</properties>
</project>
Now I can remove jar from repository. But can I remove by command line without creating pom.xml?
I'm using 3.2.5 (windows x64).
The dependency:purge-local-repository goal, up to the current version 2.10, needs to be executed on a Maven project:
Requires a Maven project to be executed.
One of the purpose of this goal is to remove from the local repository the dependencies of a Maven project, so it needs one to execute. This explains the error you have.
However, with this plugin, it is possible to specify a manualInclude parameter, which will remove any dependency specified as groupId:artifactId:version or all versions of groupId:artifactId or even everything under a groupId. Therefore, the plugin could be updated to not require a Maven project to be executed.
I went ahead, created the JIRA issue MDEP-537 to track this addition and will fix this for version 3.0.0.
As Tunaki mentioned in his answer, he requested MDEP-537 for the Dependency plugin to be able to purge without a Maven project. This has been implemented since version 3.0.0.
If you only run mvn dependency:purge-local-repository, you are likely to run an older version. E. g. my Maven 3.6.0 comes with Dependency plugin version 2.8.0.
So you should run a specific version of the plugin:
mvn org.apache.maven.plugins:maven-dependency-plugin:3.0.0:purge-local-repository -DmanualInclude=...

Running main classes from a deployed artifact with maven

I don't get it. I've set up my pom.xml to use the Maven exec plugin so I can execute some of the classes in my project with the correct classpath, -D defines and -javaagent. So from a shell with the classes built in ./target/classes etc.. I can run the main() methods using
mvn exec:java -Dexec:mainClass=classWithAMainMethod
All good so far.
Now I want to ship my project(a jar artifact) and I still want to be able to use the configuration I've put in the pom.xml for running the classes with the correct arguments etc.. How do I do it? Is there some way of staying
mvn -artifactJar=MyArtifact.jar exec:java -Dexec:mainClass=classWithAMainMethod
when all I have is MyArtifact.jar(Or a maven repository with MyArtifact.jar in it)??
I've tried the following:
Get the jar with the dependency:get goal and unzip it. I can't do anything with it
as the pom.xml seems to end up in META-INF/maven in the artifact jar. Is there any way of using it?
Creating a dummy pom where I want to run my project with a single dependency on my projects artifact. I can then use exec:java to run the main classes but it's dosen't uses the configuration from my projects pom.
Thanks.
The AppAssembler plugin worked out quite well for me. I replaced the exec plugin config in my project's pom with something like this in the build section:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>appassembler-maven-plugin</artifactId>
<version>1.2.2</version>
<configuration>
<repositoryLayout>flat</repositoryLayout>
<repositoryName>lib</repositoryName>
<extraJvmArguments>
-Djava.rmi.server.hostname=localhost
-javaagent:${spring.javaagent.jar}
</extraJvmArguments>
<programs>
<program>
<name>foo1</name>
<mainClass>net.foor.FooMain</mainClass>
</program>
...
</configuration>
</plugin>
In Eclipse I created an external tools launcher to run the resulting scripts from target/appassembler/bin
On the machine I wanted to deploy to(Assuming access to the internal Maven repository where my artifact+dependencies have been installed/deployed):
First use wget or mvn dependency:get to get a copy of my artifact jar.
Extract the pom. unzip -j artifact.jar */pom.xml*
Run mvn appassembler:assemble -DassembleDirectory=.
Move the artifact.jar into the ./lib directory
Set execute permissions on generated shell scripts in ./bin
Have you tried using something like onejar?
That sounds like what you're looking for.

Categories

Resources