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.
Related
Working with a multi-module maven project. Want to have separate maven compile & install logs. Thus executing steps as follows:
mvn clean compile
Followed by
mvn install
to run other phases in install lifecycle. Since maven-compiler-plugin(version 3.1) is capable of incremental compilation, is this execution right, or will it produce unexpected results? Do not wish to change execution if it works as expected(without skipping or compiling any file twice).
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)
I have a big server project, and some related UnitTest file using maven.
When I added the new UnitTest for some java file, I only want to build the current UnitTest and run it, I don't want to build the whole server project from the beginner.
But when I using the:
mvn test -Dtest=NewUnitTest
I still need to wait for a long time to build the whole server project.
Is there a way to only build the new added UnitTest file?
Thanks.
Try the following one:
mvn compiler:testCompile surefire:test -Dtest=NewUnitTest
compiler:testCompile - will compile your tests (if you need it)
surefire:test - will execute your tests
how are you doing this?
If you run a clean and then compile it will delete everything and then compile.
If you simply run mvn test-compile then it should just compile anything that is outdated including any test code
I am assuming that you have multi-module build. Just run (from the root):
mvn clean test -Dtest=NewUnitTest -pl :<the artifactId of the project you want to test>
Obviously, if the project you are trying to test depends on other artifacts in your reactor, you may need to build these too if they can't be fetched from a centralized repository. Just add the "also-make" flag (-am):
mvn clean test -Dtest=NewUnitTest -pl :<the artifactId of the project you want to test> -am
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.
I have a Maven Java project with many modules and one meta-modul. And I want to run test. I use: mvn test command in console. But my test fails when compilation, because classes in other modules are not found. But in IDE Eclipse no errors. How can I fix that?
if you have well defined dependencies, use mvn install instead of mvn test. Running tests is included in install phase too and you'll get the modules you need for compilation into local maven repository.
Try to run mvn install. This compile, packages and runs tests. Then when everything is compiled you can probably run mvn test only. But you should not (IMHO) because only when you are running the full process you are sure that the newest versions of your classes are being tested. Do not worry about efficiency: maven does not compile classes if it already have them compiled. Only mvn clean install will rebuild everything from scratch.
Your models need to be free of dependency cycles!
Try to run mvn test from the folder where the parent pom is located.
(details)
If you do not have a parent pom with sub modules. Then you must first run mvn install for all the other of your modules where the module you want to test depends from.
(Eclipse does not need this, because it can resolve dependencies to other open projects directly)
But if all the modules belong to an single release cycle (all the modules will be released togeter with the same version) then it is may a better approach to use parent and child modules/pom -- because then you can run mvn test or mvn install for the parent pom, and maven will do it for all the childs in the right order. -- After you have installed all other modules, you can run mvn test on a single module until you update an other modul. -- Then you will need to install this updated modul too, or better run install for the parent.