Dependency library after ant task in Maven2 - maven-2

I have a system dependency on a local jar which is compiled by a maven ant execution task using maven-ant-run plugin. How can I make sure that the ant task is executed before executing the dependency task. I am using system scope for a relative path where the jar is present after compilation.

It doesn't depend upon the scope of dependency, it depends that with which life-cycle phase you have bind the execution of the goal for any specific plug-in.
One possibility is to create a multi-module project with two modules. Module 1 should compile and place that dependency before building Module 2 (which will be having your existing project)

Related

How to automatically generate JAXB classes on build in IDEA?

I have an IDEA project that uses auto-generated JAXB classes from .xsd files. I have “client” and “server” modules that include a “common” module that contains, among other things, the JAXB classes.
I do not want to keep generated code under source control, but if the generated java classes do not exist, “client” and “server” modules do not compile. How to make IntelliJ automatically run JAXB before building?
There is no direct way to do it only with IntelliJ IDEA, you will need to use Ant or Maven or some other external process that will perform the code generation.
Check out jaxb2-maven-plugin.
In IntelliJ IDEA you can execute Maven or Ant before compilation.
In the build system's tool window you can bind a phase or a plugin goal to IDEA's build process.
For example the jaxb2-maven-plugin can be executed Before Rebuild or Before Build with a secondary click on the goal:
Another option would be to bind the goal to a lifecycle phase and execute the phase like 'generate-sources' before rebuild. In case of the jaxb2-maven-plugin the goal xjc is by default bound to the generate-sources phase of Maven.

how to run only parent pom.xml in maven multi-module project

I have maven multi-modules project. At the parent level, i have some java files. And in the parent pom.xml, at the package phase i do some stuff.
Usually, when i run mvn package at parent level, the package phase of parent pom will be run and all the modules will be packaged as well.
I am looking for a way that allow me to do these (when i run mvn package):
allow me to run only paren pom.xml (the script at the package phase), not the modules. This is the 1st priority.
allow me to run paren pom.xml and some particular modules (like module 1, module 2 BUT not module 3 , module 4).
Can i use profile for those issue?
Thanks.
While I agree with the fact that you may not have optimal project structure, the answer is that Maven 2.2.1 has an option "--non-recursive" which satisfies your first requirement:
-N,--non-recursive Do not recurse into sub-projects
So something like this:
mvn --non-recursive clean compile
Why do you want to have java code on the top level? In my opinion this is not a very good idea. Have your code in the subprojects and let the top-level project be responsible for holding the general information and configuration of the entire project.
If you have some base-library code in the top-level project now, you can put it in a sub-project and set up dependencies between the projects.
Take a look at Maven parent pom vs modules pom
The nature of your question indicates that your project structure may not be optimal.

How can i run maven tests against a previous deployed artifact of the same artifact?

I have an artifact abc which has some tests. I have different versions of abc within my repository. I now want to be able to run the latest tests against the 'old build' of the project.
I tried to add the artifact itself to the test dependencies but this (of course) results in a cyclic reference error of the maven reactor when building the tests via:
mvn compiler:testCompile
mvn surefire:test
Is there any smart way to run tests against a previous old build/artifact?
Must i create a new pom.xml in which i define the solo test execution?
Or should i add a postfix to my current artifact when executing the tests? (This would avoid a cyclic reference error)
Separate the tests out into a separate module/project that depends on the classes it tests. Then create separate profiles where you change the dependency to be on older releases.
The problem I foresee with what you're trying to do is that the package phase comes after the test phase of the maven lifecycle. Which to me implies that maven runs unit tests against the compiled classes and not the physical jar file (generated in the package phase). You'll therefore have to replace the contents of the projects /target/classes folder with the classes in the "older" jar.

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).

Maven plugin executing another plugin

I'm trying to create a new plugin to package my latest project. I'm trying to have this plugin depend on the maven-dependency-plugin to copy all my projects dependencies.
I've added this dependency to my plugin's pom, but I can't get it to execute.
I have this annotation in my plugins main Mojo:
#execute goal="org.apache.maven.plugins:maven-dependency-plugin:copy"
I've tried a few other names for the goal, like dependency:copy and just copy but they all end with a message saying that the required goal was not found in my plugin. What am I doing wrong?
Secondary to this is where to I provide configuration info for the dependency plugin?
Use the Maven Mojo executor by Don Brown of Atlassian fame to run any other arbitrary plugin.
The Mojo Executor provides a way to to
execute other Mojos (plugins) within a
Maven 2 plugin, allowing you to easily
create Maven 2 plugins that are
composed of other plugins.
Have you tried to create your own packaging type? Then you can define your own lifecycle mapping, i.e. bind goals to phases. In this case you can bind the dependency:copy-dependencies goal to your packaging phase and you don't have to wrap the goal into your own Mojo.
See also: How do I create a new packaging type for Maven?