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.
Related
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.
Generally i follow below maven commands to build and run my project.
mvn clean
mvn clean verify
or
mvn clean install
mvn spring-boot:run
My doubt is in which maven life cycle, dependencies get downloaded from maven central repository to my local .m2 repository.
I went through below mentioned maven life cycle but no where i found that in this steps dependency gets downloaded.
validate
compile
test
package
verify
install
deploy
Please explain it would be really helpful.
When you create a maven project, before validate there is step 'prepare-resources' which copies resources. Also when you do maven clean it will download dependencies. Read this link for more details
https://www.tutorialspoint.com/maven/maven_build_life_cycle.htm
The other answer here of prepare-resources is incorrect. That may be confusing the downloading of Maven plugins and their dependencies, but not the project's dependencies.
They actually are downloaded during the compile lifecycle.
Here is an example of a project where the only dependency is GSON, and I just finished running the process-resources lifecycle, the one that immediately precedes compile lifecycle. The only things present in my .m2/repository directory are things required by the default Maven plugins. Note that there is no com folder, which is where GSON would have been downloaded to.
After running mvn compile, the next lifecycle, a lot more dependencies are downloaded, including GSON:
I want to add jpoller.jar as a maven dependency, so I edited my pom.xml file to include it:
<dependency>
<groupId>org.sadun</groupId>
<artifactId>jpoller</artifactId>
<version>1.5.2</version>
</dependency>
Now, as expected, when I compile from the command line I get an error because jpoller isn't in any of the repositories I have listed in my pom, nor could I find one for it. Although I could create a repository for it, I'd rather not at this point. Thus, I get the following error:
[INFO] Failed to resolve artifact.
Missing:
---------- 1) org.sadun:jpoller:jar:1.5.2
Try downloading the file manually
from the project website.
Then, install it using the command:
mvn install:install-file -DgroupId=org.sadun -DartifactId=jpoller -Dversion=1.5.2 -Dpackaging=jar -Dfile=/path/to/file
How can I do this from the M2Eclipse plugin on machines where the maven CLI isn't available?
How can I do this from the M2Eclipse plugin on machines where the maven CLI isn't available?
Well, simply do it from Eclipse. First, get that jpoller jar and save it somewhere on your file system. Then, in Eclipse, from the top bar, Run > Run Configurations... then right-click on Maven Build and configure the New_configuration freshly created:
Select an arbitrary Base directory
Fill the Goals with install:install-file
Add parameters for each required parameters, without the -D prefix (e.g. file as Parameter name and /path/to/file as Value and so on for groupId, artifactId,packaging and version).
And run this configuration. Or... just install Maven.
The install command automates the creation of a folder structure in ~/.m2 and pom.xml file for the dependency artifact. This can be done manually. OR You can simply copy the ~/.m2/{group}/{artifact} folder from a machine that does have mvn installed.
Edit: This tool will help you find public repositories for a given dependency.
Edit2: See http://maven.apache.org/guides/mini/guide-coping-with-sun-jars.html for an explination of the process of installing dependencies manually. Note that most sun jars are now available in the java.net repository http://download.java.net/maven/2/
I've just made a java project with maven:
mvn archetype:create -DgroupId=com.company -DartifactId=\
myproject -DarchetypeArtifactId=maven- archetype-quickstart
Then need to import a 3rd party jar, path is ~/Downloads/json-simple-1.1.1.jar, I tried the following commands:
mvn -e install:install-file -Dfile=~/Downloads/json_simple-1.1.1.jar\
-DgroupiId=org.json.simple - DartifactId=json_simple -Dversion=1.1.1 -Dpackaging=jar
but an error occoured:
[ERROR] BUILD ERROR
[INFO] Missing group, artifact, version, or packaging information
I'm using maven 2.2.1, java 1.6.0_35 and Ubuntu 11.10.
The dependency is already in maven central repository. Just the following dependency to the pom of your project and you are done:
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
You don't need to install json_simple locally. It is in Maven Central. Go to http://search.maven.org and search.
groupid: com.googlecode.json-simple
artifactid: json-simple
version 1.1.1
It is better to satisfy a dependency from Central rather than relying on one you installed locally. For a start, it will make it easier for other people to build your code.
As for the build problem, I suspect that you have made a mistake in the dependencies section of your project's "POM.xml" file.
Don't remember the exact syntax but you can di the following:
Add thus as a dependency in the pom file of the project you want to build
The build will fail as this jar is not part of the repo yet
But maven will also generate the command to install the jar in repo copy paste that, change the path the jar and execute.
This should solve the purpose.
But looks like this jar is already part of some repo.gGoogle it out might just need to add a new repo
I cloned an open source library project from Github, that I'd like to use for my own Java project. This library depends on some other jars that cannot be found in any public repository. This causes mvn package to fail:
[ERROR] Failed to execute goal on project commons-gdx-core:
Could not resolve dependencies for project
com.gemserk.commons.gdx:commons-gdx-core:jar:0.0.11-SNAPSHOT:
The following artifacts could not be resolved:
com.badlogic.gdx:gdx:jar:0.9.8-SNAPSHOT,
com.gemserk.animation4j:animation4j-core:jar:0.2.2-SNAPSHOT,
com.gemserk.vecmath:vecmath:jar:1.5.3-GEMSERK:
Could not find artifact com.badlogic.gdx:gdx:jar:0.9.8-SNAPSHOT -> [Help 1]
I think I can get these jars from other sources but I don't know how to tell maven where to look for them.
as example, we have Oracle JDBC driver. You must install it into your local maven repository.
Download your lib to local folder (i.e.: Path/to/private/library.jar)
mvn install:install-file -Dfile= -DgroupId=
-DartifactId= -Dversion= -Dpackaging=jar
Or, a more advanced way, if you have a Maven Repository (like Sonatype Nexus), you can deploy it on repository and map it on your project. But, I think that you need the first option.
Use the repositories and repository tags to point to the private repositories in your pom file or update your settings.xml in the same way. Maven will download the artifacts once you tell it where to look.
Answers from apast and Chris are both correct. But if you change your computer or clean up your local repository, you project still can't be compiled. What I suggest is using system dependencies and add the jar under git version control. Here's an example.