Running maven without pom (but with text input on CLI) [duplicate] - 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

Maven test for only provided package

I have a multi-module maven project. I want to test only one package but that package may depend on other packages.
For better explanation, I created an example GitHub repository:
GitHub Example Repository
Running mvn -pl module test can cause problem because of dependencies of module. Instead of that, I must run mvn -pl module -am test but this command run test for my module and all dependents modules (in my example, run test for dependent-module-1 and dependent-module-2).
Performance is crucial for me. I don't want to run test for dependents package when I want to run tests for only one module. So I decide to convert my command to:
mvn clean
mvn -pl module -am compile test-compile (or mvn -pl module -am package)
mvn -pl module test
It doesn't works and give errors because of not provided artifact.
I search a lot and I don't find anything. Please consider that I don't want to use mvn install at all. I don't want to move it to my repository.
Is it possible to help me? I don't know what to do.
Thanks.

fetch all maven dependencies including plugin dependencies

I'm trying to get maven to download all dependencies (compile, test, plugins, etc.) so that I can avoid having our dockerized builds waste unnecessary time downloading them over and over again.
We have dockerized our maven build so that we can run it from our jenkins without having a lot of build specific dependencies installed on the jenkins machine (Java, redis, maven dependencies, etc.). Our build relies on incremental docker builds that only executes steps that actually need re-running.
Our main build is a DockerFile that has several steps to install the jdk, maven, etc. Then it does a
COPY ./pom.xml /opt/inbot-api/pom.xml
RUN mvn dependency:copy-dependencies clean
This will download dependencies to the local maven repository and then cleans out the target directory.
Then we copy the source tree to the image and run the full build.
COPY ./src /opt/inbot-api/src
RUN mvn -e clean install
The general idea is that on a clean machine, docker will execute all the RUN steps but on incremental builds it will only rerun things that need re-running. After each run step, it stores an intermediary image. So, if the pom file doesn't change, there's no need to rerun the dependency fetching step because it would produce the exact same outcome. So, instead it loads the cached intermediary image with all the dependencies already downloaded. This is exactly what we want.
There's a lot more to our DockerFile that is not so relevant here but ultimately it produces a docker file with our compiled artifacts, an nginx config and all our runtime dependencies that we can deploy to ECS.
This nearly works except the mvn clean install still downloads additional plugin dependencies on every build. So, these are dependencies that the copy-dependencies step does not cover.
My question, how do I get RUN mvn dependency:copy-dependencies clean to download all dependencies including the plugin dependencies. I've seen people actually do a mvn verify clean instead of mvn dependency:copy-dependencies clean but that is kind of slow in our case. I was wondering if there was a better way to do this.
I'd appreciate any feedback on how to improve this.
Update
I now do a
RUN mvn -B -T 4 dependency:copy-dependencies dependency:resolve-plugins dependency:go-offline clean
And it still downloads more stuff with the mvn clean install after that. A mvn -o clean install still fails, despite the dependency:go-offline. So, it seems this plugin is broken.
This works for me, no other dependencies to download:
RUN mvn -B dependency:resolve dependency:resolve-plugins
For the plugin i would suggest to use mvn dependency:resolve-plugins
See the documentation: https://maven.apache.org/plugins/maven-dependency-plugin/
I almost resolve with this:
RUN mvn install -DskipTests dependency:resolve dependency:resolve-plugins

difference between mvn clean and install commands

I am using maven for the build purpose and normally we use the maven command mvn clean -Dmaven.test.skip=true package only to build the web application. I know we can use the mvn install command also to build a web application. But can anyone provide me with the exact difference between these two commands?
I found some notes on the clean and install commands. But i just want to know what's the advantage of using mvn clean command instead of using install command.
The main different between mvn clean -Dmaven.test.skip=true package and mvn install is that the first command line cleans the target directory and packages without running the tests. The second one compiles, tests, packages and installs the JAR or WAR file into the local repository at ~/.m2/repository.
Maven has this concept of Maven Phases. Please go through the Maven Phases of this doc. So when you run a phase (say maven phase x) all the phases up to that phase is executed (that is phase 1 to phase x).
You need mvn clean to clean up artifacts created by prior builds. mvn package will package your code into your specified format in your POM. mvn install will also install the package made by Maven into the local repository.
Also note that clean and site are not part of phases of the default life-cycle. You have to fire it before your package or install command. Needless to say ordering does matter here.
As explained here.
clean is its own action in Maven. mvn clean install tell Maven to do the clean action in each module before running the install action 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 dependency for multi-module project

I have a strange issue with maven.
I'm running a dropwizard project that has multiple modules.
Project
-> ServiceModule1
-> ServiceModule2
-> ModelsModule
-> TestModule
All modules depend on the test module, and all of the service modules depend on the models module.
I'm using a test-jar to distribute the test module since all of the fixtures live in there.
So when I package my project, I do this:
cd testModule
mvn jar:test-jar
cd ..
mvn package
This works fine, except it means every time I want to package my project I have to run all the tests. If I switch to
mvn package -Dmaven.test.skip=true
I get a failure because my modules start to look for their dependency jars in maven central.
This is really frustrating, since the tests depend in a database and I don't want to install a database on every web server.
What should the "correct" setup be?
Just to be clear, if your ServiceModule1 wants to depend on ModelsModule, you can build it in two steps,
$ cd ModelsModule
$ mvn clean install
$ cd ..
$ cd ServiceModule1
$ mvn clean package
Please note install in step2,
In other words, it is not mandatory that all your lib should be in maven central. But it is mandatory that all of them should be in your local repo. install is a goal which can install a lib to your local repo.
Now this link will show you how to install a test jar to your local repo.

Running Maven goals in a reactor project without doing "mvn install"

I'm trying to execute a Java file in a subproject in a maven reactor project. This is similar to the question Maven exec:java goal on a multi-module project, but unless I do a mvn install then the exec plugin can't find the class that I'm trying to run in the subproject.
Perhaps I misunderstand the intended workflow of mvn install, but having to do mvn install every time I make changes really complicates the workflow.
When I execute the file from Eclipse, Eclipse sets up the classpath correctly (i.e. module1/target/classes, module2/target/classes) and I want to emulate this behaviour from the command line. I thought doing mvn -pl exec:java -Dexec.mainClass=... would set up the classpath in this way, but the class is not found in this case.
The classpath isn't the problem in that case. But you have to compile your classes (e.g. at least run mvn compile).
If you run your application within Eclipse, Eclipse will do the compile work, on the commandline you have to explicitly call that command.

Categories