specify pom.xml in mvn command and mix goals of other project - java

I have multiple questions.
Can I specify the pom.xml in mvn command?
Can I mix the goals of another project while executing mvn command on current project ?
Eg: mvn clean-otherproject comple-otherproject instal-otherproject compile-thisproject
I can do this with multiple mvn commands, but Can I do this in single maven command.

Just mvn --help would have answered the first question:
mvn -f otherPomFile.xml
No. You can simple execute the phases for the current project you are in. You can give multiple phases like
mvn clean install site site:deploy

For the first question, see khmarbaise's answer
If you want to build more than one maven project in one step, you must use modules.
In a multi-module project, if you call mvn install from the top project, all sub modules are built, unless you use the advanced reactor options (e.g. mvn install -pl util -am only builds the module 'util' and it's dependencies)

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.

Maven works in Command Prompt, but not in Power Shell

I am very new to Maven and initial stages where i am exploring how to create a java EE project that i can host in a server. Maintain it through Maven.
First question is asked is how do i create the same structure of a java project that is created by Eclipse when i create a new Maven project in command line in Power Shell?
My findings -
Please see the image snippet
I understand that once we have a project crated. If you want to add modify how your project should build, we can modify that, or we can add new dependencies or many other things from the POM.xml.
But initially atleast it should create the structure of the maven project with a minimal pom.xml right ?
I used the command mvn archetype:generate .... In the Command Prompt the project was created correctly. But I need Maven work in the Power Shell, and this produces an error:
The goal you specified requires a project to execute but there is no POM
in this directory ...
Thanks in Advance
See tutorial. It is pretty good:
https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html
To create a project structure:
mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false
For PowerShell, use quotes for -D, as follows:
mvn -B archetype:generate "-DarchetypeGroupId=org.apache.maven.archetypes" "-DgroupId=com.mycompany.app -DartifactId=my-app"

Want to build project with commandline mvn but have to use Maven update in eclipse first

I'm working with multiple projects in Eclipse. because I want to automate the building I want to script the building process.
Unfortunately I cannot do the same actions on the commandline as in Eclipse.
So a common problem is that when a new function from a referenced project is used, I cannot build the project on the commandline with mvn. I use the command:
mvn clean install -U
But this command will give a build failure until I do a Eclipse Maven Update from the eclipse GUI. After that I can build the project again.
I also tried all the other commands I came across Stackoverflow:
mvn eclipse:eclipse
mvn dependency:resolve
So I just want to that Maven Update command in eclipse from the commandline so I can build from the commandline. If anyone could tell me what I'm doing wrong that would be awesome.
Thx in advance
Update for more clarification:
The project structure is:
Rest-service, Framework-service, Framework-model
Framework-model is referenced in the pom file by Framework-service and Framework-service is referenced by Rest-service. The other projects are not relevant to the problem.
When a function is added to Framework-model and used in Rest-service it gives an compilation error in eclipse and when I build with mvn clean install -U, although Maven install in eclipse is succesful but I think it is still using the old compiled code. After a Maven Update command in eclipse the compilation error is gone. And mvn clean install -U from the commandline also works.
How could I do a Maven Update command in the commandline? If mvn clean install -U should also do a Maven Update command, what settings should I check?
Another update: So this weekend I tried different things and running mvn compile before the mvn clean install -U command gives a different output. And finds the new function. But as I read maven, I thought install should also do the previous steps. How is this possible?
Eclipse's Maven plugin uses the maven version configured in Preferences/Maven/User settings. If you have a different version of maven in your Eclipse's settings than the one on your PATH variable, you could have different outputs. Maybe try and check that.

Create a list of artifacts that are build by a maven project

Is there a way to create a list of all artifacts that are created by running
mvn clean install
similar to the output of :
mvn mvn dependency:tree/list
There is a way to do that:
mvn -q -Dexec.executable='echo' -Dexec.args='${project.groupId} ${project.version} ${project.artifactId} ${project.packaging}' --non-recursive exec:exec
If you want it for all artifacts in your project including sub-folders remove --non-recursive.
It can be achieved by writing a maven extension by extending org.apache.maven.AbstractMavenLifecycleParticipant which will run at the end of maven build afterSessionEnd and using MavenSession.getAllProjects(), Artifact.getArtifact(), getAttachedArtifacts(), getGroupID(), getArtifactId(), getClassifier(), getType() you can loop through all the projects and get the details for each artifact generated.
make sure to set the correct profile(if you have any) to not miss any info about the artifacts like reported here get classifier/id of maven assembly artifact

How is "mvn clean install" different from "mvn install"?

What is the difference between mvn clean install and mvn install?
clean is its own build lifecycle phase (which can be thought of as an action or task) in Maven. mvn clean install tells Maven to do the clean phase in each module before running the install phase for each module.
What this does is clear any compiled files you have, making sure that you're really compiling each module from scratch.
Maven lets you specify either goals or lifecycle phases on the command line (or both).
clean and install are two different phases of two different lifecycles, to which different plugin goals are bound (either per default or explicitly in your pom.xml)
The clean phase, per convention, is meant to make a build reproducible, i.e. it cleans up anything that was created by previous builds. In most cases it does that by calling clean:clean, which deletes the directory bound to ${project.build.directory} (usually called "target")
You can call more than one target goal with maven. mvn clean install calls clean first, then install. You have to clean manually, because clean is not a standard target goal and not executed automatically on every install.
clean removes the target folder - it deletes all class files, the java docs, the jars, reports and so on. If you don't clean, then maven will only "do what has to be done", like it won't compile classes when the corresponding source files haven't changed (in brief).
we call it target in ant and goal in maven
To stick with the Maven terms:
"clean" is a phase of the clean
lifecycle
"install" is a phase of the
default lifecycle
http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html#Lifecycle_Reference
Ditto for #Andreas_D, in addition if you say update Spring from 1 version to another in your project without doing a clean, you'll wind up with both in your artifact. Ran into this a lot when doing Flex development with Maven.

Categories

Resources