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.
I honestly don't know anything about java, I'm just trying to build the latest version of this project to try and work around bugs in the version that is packaged by my OS. The project in question:
https://sourceforge.net/projects/tuxguitar/
I tried running mvn compile, mvn clean package, and mvn clean install but no jar file was produced in the source tree. The readme has no instructions for building. Any help would be greatly appreciated!
Thanks!
To produce jar you need at least package phase, you can get there by doing:
mvn clean package
(clean is to remove stale data, it is a good practice to use it always with maven) and you jar will be in target directory.
mvn complite just complite the project and no package.
answer : mvn clean package.this will clean,complite and package the project,after finished, you can found the package(jar or war) into the $PROJECT_HOME/target/ folder.
I usually run mvn clean install - which in addition will add the built jar into your local maven artifact repository.
If I know the coordinates of an artifact, and a name of the class inside that artifact, can I make Maven run the class, including all of its dependencies on the Java classpath?
For example, suppose a coworker told me about a tool I can run, which is published to our internal Nexus with the artifact coordinates example:cool-tools:1.0.0. I used this answer to download the artifact. Now, I know that the main class name is example.Main. But if I just go to the artifact's download location and run java -cp cool-tools-1.0.0.jar example.Main, I get NoClassDefFoundErrors for any dependencies of cool-tools.
I'm aware of the maven-exec-plugin, but as far as I can tell that's only for projects where you have the source. Suppose I don't have access to the source, only the Nexus containing the tool (and all its dependencies). Ideally, I'd do something like mvn exec:exec -DmainArtifact='example:cool-tools:1.0.0' -DmainClass='example.Main', but I don't think the exec plugin is actually capable of this.
ETA: To be clear, I do not have a local project / POM. I want to do this using only the command line, without writing a POM, if possible.
There is no out-of-the-box solution for your task. But you can create a simple script to solve it:
Download pom.xml of your tool from the repo.
Download jar of your tool.
Download all its dependencies.
Run java against resolved libraries.
Command line:
> mvn dependency:copy -Dartifact=<tool.group.id>:<tool.artifact.id>:<tool.version>:pom -DoutputDirectory=target
> mvn dependency:copy -Dartifact=<tool.group.id>:<tool.artifact.id>:<tool.version> -DoutputDirectory=target
> mvn dependency:copy-dependencies -f target/<tool.artifact.id>-<tool.version>.pom -DoutputDirectory=target
> java -cp target/* <tool.main.class>
Directory ./target will contain your tool + all dependencies.
See details on dependency:copy and dependency:copy-dependencies mojos.
Edit
As alternative, you can build classpath using libraries in the local repo by:
> mvn dependency:build-classpath -DincludeScope=runtime -f target/<tool.artifact.id>-<tool.version>.pom [-Dmdep.outputFile=/full/path/to/file]
See details on build-classpath mojo.
You could download the pom from the repository using wget, for instance. Then resolve the dependencies, and build the classpath exporting it to a file using Maven. Finally, execute with Java and the built classpath using something like bash backticks to use the content of the file.
Just like in this answer.
For me the first anwer almost worked, but I needed to slightly adjust the script. In the end I came (on windows machine) to following solution:
> mvn dependency:copy -Dartifact=<tool.group.id>:<tool.artifact.id>:<tool.version>:pom -DoutputDirectory=target
> mvn dependency:copy -Dartifact=<tool.group.id>:<tool.artifact.id>:<tool.version> -DoutputDirectory=target
> mvn dependency:copy-dependencies -f target/<tool.artifact.id>-<tool.version>.pom -DoutputDirectory=target
> cd target
> java -cp target/*;<tool.artifact.id>-<tool.version>.jar <tool.main.class>
On Unix/Linux machine in the last command the semicolon ";" must be replaced with colon ":".
When input arguments must be provided, just put them in the last script line:
> java -cp target/*;<tool.artifact.id>-<tool.version>.jar <tool.main.class> arg1 arg2 ...
u can use IDEs like Intellij idea which automatically resolve dependencies as u write them in your pom
As it has been mentioned by others already there is no solution without creating an extra POM.
One solution could be to use the Maven Shade Plugin in this POM: "This plugin provides the capability to package the artifact in an uber-jar, including its dependencies"
I think the Executable JAR is close to that what you'd like to achieve.
Folks,
As part of my nightly build for a Java application, I do the following:
$ cd dirContainingPOM.XML
$ svn update
$ rm -rf target
$ mvn package
Essentially, I get the latest version of the source code, delete all the files in the target directory, and build the jar package.
The question I have is if it is a good practice to delete everything in "target" directory or is it just an overkill. Is maven doing it automatically for me?
Thank you in advance for your help.
Regards,Peter
It's a good practice but you easily achieve the same thing by specifying the clean target in your maven command.
Instead of performing ...
mvn package
Try the following ...
mvn clean package
This will effectively negate the need to perform the rm -rf on your target directory.
I am trying to package my project. But, it automatically runs the tests previous do performing the packaging. The tests insert some content in the database. This is not what I want, I need to avoid running tests while package the application. Anybody knows how run the package with out test?
Run maven with
mvn package -Dmaven.test.skip
Just provide the command mentioned below, which will ignore executing the test cases (but will compile the test code):
mvn package -DskipTests
you can add this plugin configuration to your pom if you do not want to set command line arg:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
Note that -Dmaven.test.skip prevents Maven building the test-jar artifact.
If you'd like to skip tests but create artifacts as per a normal build use:
-Dmaven.test.skip.exec
If you are trying this in Windows Powershell, you will get this error:
[ERROR] Unknown lifecycle phase ".test.skip=true". You must specify a valid lifecycle phase or a goal in the format...
The reason for this is, in Powershell the "-" has special meaning and it is causing problem with maven.
The solution is to prepend it with a backtick (`), like so..
mvn `-Dmaven.test.skip=true install
Reference: http://kuniganotas.wordpress.com/2011/08/12/invalid-task-test-skiptrue-you-must-specify-a-valid-lifecycle-phase/
<properties>
<maven.test.skip>true</maven.test.skip>
</properties>
is also a way to add in pom file
In Inllij IDEA there is an option also to skip test goal.
You can pass the maven.test.skip flag as a JVM argument, to skip running tests when the package phase (and the previous ones in the default lifecycle) is run:
mvn package -Dmaven.test.skip=true
You can also pass the skipTests flag alone to the mvn executable. If you want to include this information in your POM, you can create a new profile where you can configure the maven-surefire-plugin to skip tests.
You only have to provide
-Dmaven.test.skip
You no longer need to append =true.
Answering an old and accepted question here. You can add this in your pom.xml if you want to avoid passing command line argument all the time:
<properties>
<skipTests>true</skipTests>
</properties>
You can add either -DskipTests or -Dmaven.test.skip=true to any mvn command for skipping tests. In your case it would be like below:
mvn package -DskipTests
OR
mvn package -Dmaven.test.skip=true
just mvn clean install -DskipTests
A shorthand notation to do maven build and skip tests would be :
mvn clean install -DskipTests
Below two commands are most useful
mvn clean package -Dmaven.test.skip=true
mvn clean package -DskipTests
Thanks
Tests should always[1] run before package. If you need to turn off the tests, you're doing something wrong. In other words, you're trying to solve the wrong problem. Figure out what your problem really is, and ask that question. It sounds like it's database-related.
[1] You might skip tests when you need to quickly generate an artifact for local, development use, but in general, creating an artifact should always follow a successful test run.
you can use any maven goals like package / clean install
Solution: 1 ( package Goal )
mvn package -Dmaven.test.skip
mvn package -Dmaven.test.skip=true
mvn package -DskipTests
Solution: 2 ( clean install goals )
mvn clean install -Dmaven.test.skip
mvn clean install -Dmaven.test.skip=true
mvn clean install -DskipTests
Solution: 3 ( in pom.xml file )
you can use maven-skipping-tests in pom file and no need to provide
the attributes like DskipTests,Dmaven.test.skip
Pom.xml
<properties>
<java.version>1.8</java.version>
<maven.test.skip>true</maven.test.skip>
</properties>
Later you can use maven command : mvn clean install/mvn package
Reference: https://www.baeldung.com/maven-skipping-tests
For maven package without infecting maven test:
<properties>
<maven.test.failure.ignore>true</maven.test.failure.ignore>
</properties>
In Intellij, go to View -> Tool Windows -> choose Maven Projects.
On the Lifecyle dropdown, right-click on package -> choose Create 'your-project [package]'...
Enter this value: package -Dmaven.test.skip=true -f pom.xml in the Command line field. Click Apply and a Run Configurations dropdown menu should appear along with your created custom maven command.
Those who don't want to skip the test cases.
just above the main test class comment out or delete annotation:
//#SpringBootTest
Then when Maven builds an app, it will still run tests inside this class but will not run SpringBoot app, so will not test the connection to DB and the build will be successful.
You are, obviously, doing it the wrong way. Testing is an important part of pre-packaging. You shouldn't ignore or skip it, but rather do it the right way. Try changing the database to which it inserts the data(like test_db). It may take a while to set it up. And to make sure this database can be used forever, you should delete all the data by the end of tests. JUnit4 has annotations which make it easy for you. Use #Before, #After #Test annotations for the right methods. You need to spend sometime on it, but it will be worth it!
mvn clean install -Dmaven.test.skip=true
worked for me since the -Dskip did not work anymore.
mvn package -Dmaven.test.skip=true
The best and the easiest way to do it, in IntelliJ Idea in the window “Maven Project”, and just don’t click on the test button. I hope, I helped you. Have a good day :)