Maven2 life cycle help - maven-2

I’ve built a custom Maven2 plug-in using Ant. I would like to call another maven plug-in in particular the Cargo Maven2 plug-in immediately after the custom Ant plug-in successfully completes. However, I do not want to attach the Cargo plug-in to another goal or phase. Is there a way to have them run consecutively without having to write a batch script? Is there away to have the Custom Ant plug-in call out to the Cargo plug-in?

See this discussion: Re: calling plugin in another plugin? According to the Maven developers, this is not the way plugins are supposed to work.
However, there is this interesting comment:
Plugins/Mojos should be thin wrappers around a library. You'd want to use the library directly.
Cargo is not only a Maven plugin, it also has a Java API and an Ant task. So you could probably:
call the Cargo Ant task from your Ant mojo (I think you'll just need the Cargo JARs in your plugin's classpath);
rewrite your Ant mojo in Java, and invoke the Cargo API (you'd want to look at the sources of the Cargo plugin).

The Ant script that the maven-ant-plugin executes is not really aware of Maven as such; this plugin is designed for backwards compatibility with custom Ant tasks. I cannot think of a clean way of doing what you want though there may be some kind of hack that allows you to do it.
It should also be possible to execute a second instance of Maven from inside Ant, which runs purely the Cargo goal, but in that case you might encounter problems with locked files and the like. The way to do it would be to just use an tag in your Ant script and call the "mvn" executable with the appropriate goals as arguments.
The cleanest way is to simply bind the Cargo goal to a phase of the build, and have that run after Ant is finished. I do not see any disadvantage to that approach - you haven't really stated any specific reasons why you want to avoid it.

You might be interested in the two following maven
The Mojo Executor plugin and
The GMaven plugin
The GMaven plugin lets you write maven plugins using groovy. This gives you full access to ant using the Ant Builder, it is a very easy way to write ant scripts in Groovy.
Then in this Groovy mojo you could call any maven mojo using the Mojo Executor.
I have used those in several custom maven plugin and I haven't found an easier way to write and combine mojos.

Related

Binary output and testing for Eclipse plugins

I am developing an Eclipse plugin and I use maven to coordinate my source structure. In order to compile the plugin I use the tycho extension for maven. However, I was wondering how to execute unitests.
I want to use the surefire plugin for testing as I additionally use a sonar server for source code quality management. Unitests are applyed if I use eclipse-test-plugin as package target. However, I want to make use of the default surefire plugin for applying unitests.
Now I figured out that the src/test/java that contains my unittest packages is read and compiled correctly but written into the wrong output folder. I need to have the tests in target/test-classes. However they are compiled to target/classes.
As I am new to Eclipse plugin development and maven I could not find out how to write the tests to the correct output folder. I've already tried adding and and changing the build.properties of the eclipse-plugin project. It works also fine for other projects that aren't plugin projects and do not make use of tycho.
Any help appreciated.
Regards,
Florian
Unlike standard maven projects, the convention for eclipse plugins/OSGi bundles is to have tests reside in separate projects. This is because there is no such thing as a maven dependency scope "test" in OSGi.
Thus keeping your tests inside the same project as your code under test would force you to mix up test code/dependencies an productive code/dependencies.
As you mentioned, Tycho provides a separate maven packaging type "eclipse-test-plugin" which you should use for dedicated test plugins/fragments. See https://docs.sonatype.org/display/TYCHO/PackagingTypes
There is no support in Tycho for plain unit tests residing in the same project.

How well does m2eclipse deal with maven plugins?

In general, how well does m2eclipse deal with Maven plugins that modify or amend lifecycle phases?
In particular, I have a project that has a maven-clean-plugin extension to remove an extra generated directory (not in target/) using the configuration filesets tag. This works when running mvn at the command line but not when doing a clean in Eclipse. Is there any way to get m2eclipse to process that plugin?
Another example is flexmojos; there's a lot that can be configured with the flexmojo plugin but those parameters don't seem to get imported by m2eclipse.
Is the integration solely ad-hoc? If m2eclipse embeds Maven, why can't the plugins be executed directly using the underlying pom.xml configuration?
In general, how well does m2eclipse deal with Maven plugins that modify or amend lifecycle phases?
Decently, to my experience. At least for plugin bound to phases from the default lifecycle.
In particular, I have a project that has a maven-clean-plugin extension (...). This works when running mvn at the command line but not when doing a clean in Eclipse. Is there any way to get m2eclipse to process that plugin?
What the clean plugin "extension" is doing and what you're doing (calling mvn clean from Eclipse? calling Project > Clean?) is unclear - at least for me. But maybe have a look at MNGECLIPSE-823 or MNGECLIPSE-156. And don't hesitate to clarify :)
Another example is flexmojos; there's a lot that can be configured with the flexmojo plugin but those parameters don't seem to get imported by m2eclipse.
I don't do flex so the above is too vague for me. But providing a more concrete example might help.

How make Eclipse instrument classes at build time?

Sometimes I have to perform some custom bytecode transformation.
I have used mainly asm and javaassit.
Inside eclipse usually I run my code with the -javaagent jvm parameter. Outside eclipse I use maven, ant, or the command prompt to invoke the weavers before running the application code.
But the point is that: I would like to perform instrumentation at build time inside eclipse.
What is the best way to do it?
Is there an already made plugin that I can connect to by implementing some api?
May I script this with eclipse monkey?
May I use an ant builder and invoke my weaver with it?
Should I look at the AspectJ plugin (must be huge) and try to figure out how to make my own plugin?
Should I look at the some other plugin to get inspiration?
Thanks.
You can create an annotation processor. This way you will be able to use it with ant, maven and any IDE (not only Eclipse).
Here is an example:
http://java.dzone.com/news/using-java-6-processors

Sample Ant project with all the 'features' of Maven?

The past week or two I've been studying Maven, and I like it, but there are just a few things that I require Ant tasks for, rather than Maven's complicated and scarcely-documented POM file. However, I believe Maven has a great folder structure going for it, and I like that it natively supports tests, packages the project into a jar by default, and supports a 'resources' folder, from which everything is directly copied into the jar file.
Is there a sample Ant project out there that does the same sort of thing but with an Ant build.xml script? I want the placeholder folders and hello world app and test, just like Maven does when you first create a project from its default archetype:create goal (as demonstrated in the Maven in 5 Minutes page). Or, is there an even better Ant sample project out there that does more or suggests a better folder layout?
If no such thing exists, perhaps someone can help create it in a nice detailed answer? I would be willing to host a permanently-available zip file for anyone who finds this question in the future.
mvn archetype:create
mvn ant:ant
http://maven.apache.org/plugins/maven-ant-plugin/usage.html
... but if there are really "just a few things", you may want to check out the AntRun plugin instead. I'm not trying to sell you on Maven, believe me, but since it's not clear exactly what is stopping you from trying it with your project, I guess I'm suggesting you try to push the issue a little harder.
Refer this: Why you should use the Maven Ant Tasks instead of Maven or Ivy
I also wouldn't recommend Ivy, reasons at the link above.
Quoting the Maven - Frequently Asked Technical Questions and more precisely:
How can I use Maven features in an Ant build?
The Maven Ant Tasks allow many of the
features of Maven, such as dependency
management and repository deployment,
to be used in an Ant build.
Refer to the installation page and the usage page for instructions for installing and using the Maven Ant Tasks respectively. You'll find many links to samples in the usage page and a build.xml showing most of the features in action.
An alternative (direct competitor?) to Maven Ant Tasks would be Apache Ivy.
PS: While it's definitely a good idea to adopt maven standards, even partially, I'd really think about it twice before to drop Maven (but I live in the Maven jungle for a while now - and I like it - so I'm biased).

Is there a good Rhino Javascript compiler available as Maven plug-in?

Commons JCI project doesn't seem to be maintained since a long time. Are there other options?
Maven does allow you to use Ant tasks using the AntRun plugin. So you could use the Mozilla compiler through an Ant task, such as this one. Not pretty, but I bet it will work.