configuring maven plugin with a custom packaging - maven

I have written a custom maven plugin which works in package phase of maven's default life cycle. In addition I have added a custom packaging type to it. In order to support the custom packaging type I have introduced components.xml so it will override the default maven life cycle. In the component/configuration/lifecycles/lifecycle/phases/package section of component.xml I have added my plugin to execute in package phase.
When I use my plugin I pass the configuration to the plugin through the pom.xml as follows;
<build>
...
<plugins>
<plugin>
<groupId>sample</groupId>
<artifactId>sampleArtifact</artifactId>
<extensions>true</extensions>
...
<executions>
<executions>
<phase>package</phase>
<goal>generate</generate>
<configuration>
//Configuration goes here.
</configuration>
</executions>
</executions>
...
</plugin>
</plugins>
...
</build>
Problem:
the configuration I passed into mojo as above is not getting set in the mojo. However, if I set the plugin configuration in one level below the tag(where the executions tag present), then it works. Since this plugin works in package phase I need the plugin configuration passed in through as above. Without custom packaging, the above configuration works well. Any thoughts on what I miss here?

Related

How to specify a default goal for a Maven plugin?

I've defined a Maven plugin with multiple goals. Currently users run my plugin as follows:
<plugin>
<groupId>myGroupId</groupId>
<artifactId>myArtifactId</artifactId>
<version>someVersion</version>
<executions>
<execution>
<goals>
<goal>myGoal</goal>
</goals>
</execution>
</executions>
</plugin>
but I've seen other plugins, like maven-compiler-plugin and Flyway, that don't require specifying an execution: https://flywaydb.org/getstarted/java
<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>5.2.4</version>
<configuration>
<url>jdbc:h2:file:./target/foobar</url>
<user>sa</user>
<locations>
<location>classpath:db/migration</location>
</locations>
</configuration>
</plugin>
How do I specify the goal that should run by default when users exclude the <executions> block?
AFAIK, there are not default goals for Maven plugins.
You can configure a plugin without adding a goal. But this does not execute the plugin.
The plugin must be either executed explicitly on command line (like flyway:migrate) or is executed automatically through the lifecycle (like compile:compile or jar:jar).
I assume you are using the Java5 Annotations to mark your plugin as available mojo? (and not the javadoc way of living).
The #Mojo annotation has a defaultPhase attribute.
Once a user adds the plugin into the build these defaults (if set) will be used.
The Flyway Migrate Mojo does it this way too.
The compiler plugin is a bit of a bad example, as it is part of the default plugin bindings of the maven life-cycle itself. So the phase itself will know what mojo to run.
These are the docs for the maven plugin api, the one for using annotations is nearby.
If it is not your plugin, you can put the configs you want into a parent pom into the pluginManagement section.

Maven plugins without <executions> tag

I am studying Maven plugins from the official references. I read:
You can also configure a mojo using the executions tag. This is most
commonly used for mojos that are intended to participate in some
phases of the build lifecycle.
but If I do not use the <executions> tag, like this:
<project>
...
<build>
<plugins>
<plugin>
<artifactId>maven-myquery-plugin</artifactId>
<version>1.0</version>
<configuration>
<url>http://www.foobar.com/query</url>
<timeout>10</timeout>
<options>
<option>one</option>
<option>two</option>
<option>three</option>
</options>
</configuration>
</plugin>
</plugins>
</build>
...
</project>
what happens?
Will we be able to run a goal from maven-myquery-plugin only in isolation giving a command like mvn myquery:myquery or does the plugin come up bundled in some phase/life cycle by default?
I also have been wondering if it's possible to skip the executions tag. As there is no direct answer on the official website I tried to make such a plugin with default phase bindings by myself and the answer is: no goals will be executed if they weren't specified in the executions tag. Such a plugin (goal) could be executed only by direct command: <plugin>:<goal>

Change default pom.xml and project layout of Maven

I am just starting using maven and I use Apache Maven Shade Plugin a lot. Is it possible to add these code
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.2</version>
<configuration>
<!-- put your configurations here -->
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
to default pom.xml. Yes, it can change setting.xml to make this plugin work with all project. But if I have some specific project which will not need this plugin, do I have to rewrite setting.xml again?
Another problem is that is it possible to change project layout of Maven. I use git a lot. Can I add sample .gitignore every time when I run mvn archetype:generate.
For you first issue, I think you can benefit from the parent POM:
http://books.sonatype.com/mvnex-book/reference/multimodule-sect-simple-parent.html
It's a defined POM file in which you put whatever you want. You publish it as a "pom" in your Maven repository and then, you can inherit from it in other projects. It is very convenient to lock dependency verions as well.
Your second issue seems more related to the archetype you are using than maven itself. You will probably have to create your own with a default .gitignore in it.

How do I disable the maven-compiler-plugin?

I have a maven project that uses the aspectj-compiler-plugin. I use intertype declarations so there are references to Aspect code in my Java code. Because of this, the maven-compiler-plugin fails to compile since it does not compile the aspect code.
My question is: how do I disable the maven-compiler-plugin from running because it is not doing anything useful?
There are several ways that I can get this project compiling, but they are sub-optimal:
Add exclusion filters to the maven-compiler-plugin. The plugin will still run, but it will not try to compile anything. Problem is that this breaks the ajdt project configurator in Eclipse
Move all java code to the aspectj folders. This doesn't feel right either.
You can disable the a plugin by set the phase of the plugin to none.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>default-compile</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
In Maven 3, the following will do this, for example disabling the clean plugin:
<build>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.4.1</version>
<executions>
<execution>
<id>default-clean</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
The same technique can be used for any other plugin defined in the super-POM, the packaging type, or the parent POM. The key point is that you must copy the <id> shown by help:effective-pom, and change the <phase> to an invalid value (e.g. "none"). If you don't have the <id> (as e.g. in Jintian DENG's original answer – it has since been edited to add one), it will not work, as you have discovered.
Either configure the skipMain parameter:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<skipMain>true</skipMain>
</configuration>
</plugin>
</plugins>
</build>
Or pass the maven.main.skip property:
mvn install -Dmaven.main.skip=true
The reason maven-compiler-plugin executes in the first place is because you trigger one of the default lifecycle bindings. For example if you're packaging jar using mvn package, it will trigger compile:compile at compile phase.
Maybe try not to use the default lifecycle, but use mvn aspectj:compile instead.
http://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html has more information about maven default lifecycle bindings

How to create jar archive of projects sources with maven

I include the following snippet in a projects object model
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.1.2</version>
</plugin>
according to maven.apache.org the plugin attaches the jar goal to the package phase.
However doing "mvn clean ; mvn package" does not generate a project-sources.jar in the target directory.
EDIT: Propably i do not understand the comment from the website, which i quoted: "[The source:jar goal] Binds by default to the lifecycle phase: package." I expected that, when i include the plugin section as shown above maven already binds the source:jar goal to the package phase. Am i mistaking here? What does the comment mean?
matthias.
The documentation is a little misleading. The plugin has a default execution phase of package but there is no default goal. I believe that you have specify a goal in order for the plugin to work.
You need to bind the plugin to a maven life-cycle goal for it to generate the source jar. Otherwise, you need to invoke it explicitly mvn source:jar.
As documented here, you can bind it to the jar goal.
Try this:
<project>
...
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.1.2</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
</project>
It uses then the default binding of jar-no-fork goal to package phase of the lifecycle and that's probably what you need here.

Resources