What does maven auto build do exactly? - maven-2

When I change some code I can see this pop into the console 'Maven Builder: AUTO_BUILD'. I would have expected my target folder to have been updated(re-built) but this does not seem to be the case.
Thanks,
Daniel.

The Maven Auto-Build step of the Maven Eclipse Plugin essentially executes the equivalent of mvn compile. It does not execute a clean or any lifecycle phases beyond compile. This means that a Maven Auto-Build will not generate any of the JAR files that the Maven project might be configured to generate.

Related

Does mvn clean install runs the surefire test before updating .m2 repository

How does mvn clean install works
mvn install should execute all the phases before install phase, that means it will first run the surefire test and then later update m2 respository with the dependency
Yes, this is how it works.
The phases are executed in order and the installation into the local repository is the last one.
With external dependencies it's simple: before compiling anything Maven downloads them.
With inter-module dependencies it's more complicated. It's important to understand that Maven first runs all phases for the 1st module, then runs all phases for the 2nd module, and so on. So depending on which phase you call there either will be a JAR file or there won't be.
Maven will try to give you as "high-quality" dependency as possible, meaning:
if you run mvn install - this will put 1st module's jar into ~/.m2, then by the time 2nd module starts - it already has a JAR dependency waiting in the local repo.
if you run mvn package the JAR doesn't go to local repo, it stays in target/. Thus this target/xxx.jar will be used as a dependency.
if you run mvn test there won't even be a JAR. There will only be target/classes. So 2nd module's compilation & runtime will have directory with classes & resources in its classpath (no JAR).
PS: this may actually lead to real implications. When you run tests in module2 you may be reading resources of module1 and working with their file paths. And everything will work fine in case of mvn test. But once you start running at least mvn package there won't be files anymore - all resources of module2 will be in JAR and thus can be accessed only with getResourceAsStream().

How to avoid maven compiling testClass in normale compiling

I have a default maven project:
/src/main/java
/src/test/java (include *Test.java).
When I exeucte "mvn compile", maven also tries to compile the testClasses under /src/test/java. This fails, as of some dependencies such as JUnit are under "test"-scope. Changing the scope of e.g. JUnit to "provided" everything works fine.
How can I avoid maven to compile testClasses when compiling? In my understanding, I expect to maven to compile this files only when executing "testcompile".
I am using maven 2.2.1
I believe it is nothing to do with the scope of JUnit. Normally we set JUnit's scope to test (instead of provided) and everything is just fine.
src/test/java is compiled by Maven Compiler Plugin's testCompile goal. However, you don't need to explicitly run that goal. Please have a look in topics about Maven's Lifecycle. For example, if you run maven install, it is implicitly going through many phases (e.g. compile, compile test, generate resources etc), and many of them is bounded to a default plugin goal.
If you want to avoid test source from building, from Maven Compiler Plugin's usage page, compiler:testCompile will be skipped if you turn off testing by setting maven.test.skip=true
So, if your unit tests are not yet ready, just build with Maven, with -Dmaven.test.skip=true parameter.
Just to add, this is absolutely not a good practice to assume "unit test failing" being normal during development.
Just use the skip parameter for the maven-surefire-plugin which skip tests as well as compiling the test.
The pom.xml had defined "sourceDirectory" incorrectly. Removing it fixed the issue
Instead of command "maven clean compile" , use "mvn -B -Dmaven.test.skip=true clean compile". This parameter skips test.

How to convert Ant project to Maven project

How to convert a Ant project to Maven project? A sample project that would link (a Wicket project)
Thanks
The nice part of using maven is that most standard stuff works automatically once you do things the maven way. For a simple webapp:
Create a pom with groupId, artifactId and version (packaging: war)
Add the required dependencies to the pom
move the
java sources to src/main/java,
resources to src/main/resources,
webapp content to src/main/webapp,
test content to src/test/java and src/test/resources
set the compiler compliance version using the maven compiler plugin
That should get you up 'n' running.
http://www.sonatype.com/people/2009/04/how-to-convert-from-ant-to-maven-in-5-minutes/
I don't know what your ant script looks like, but assuming its a basic script for building, you will need to create a pom.xml file for your project, add your dependencies, and then build it via maven.
For anyone who lands here in future, there is an easier way to find dependencies for maven using the file hashes. So, you won't have to guess artifact versions.
As per the below article, the idea is to generate a SHA1 checksum of the dependency that you want to find the information, then do a reverse search in Nexus repository manager using that hash. For the checksum generation, you can use Microsoft's FCIV (free) utility.
https://devreads.xyz/ant-to-maven-conversion-the-painless-method/

Maven adds class files of snapshot dependencies

on my Windows machine I do have several proeject that I build with maven. At the moment they are all in SNAPSHOT-State. When I build a project that relies on one of the other projects maven always adds the class files of the other projects to the jar.
If I build the project on my CI-Server this problem does not occur. Does anyone have an idea why maven adds the class files to my jar?
I'm using maven 2.2.1
When I build a project that relies on one of the other projects maven always adds the class files of the other projects to the jar.
This is not a default behavior and, if it happens, you're somehow telling Maven to do so. If you want to hunt potential discrepancies, check the effective-pom, the effective-settings, the active-profiles using the following goals on both machines:
help:effective-pom
help:effective-settings
help:active-profiles
Also double check how Maven is invoked on the CI machine (extra command line parameter, etc).

Can maven-exec-plugin fail the build?

I have a maven execution that runs a javascript compressor as a command line program, however, this tool also does some jslint checks as well.
If these jslint checks fail so should my build.
These errors are output as a parseable string to stdout.
If this maven plugin cannot solve this problem, I'm happy to use any other suggested.
Thanks.
To my knowledge, exec:exec will fail the build if the return code of the executed command is not 0 (or one of the configured successCodes). Maybe you can build something around this.
As an alternative, have a look at these plugins, they both can fail a build on problems:
the Jslint Maven Plugin
the YUI Compressor Maven Mojo
Depending on what you need exactly, there is also the Maven Javascript Plugin.
And also have a look at this Maven plugins for javascript question.