maven goal doesn't execute properly if plugins are defined under pluginManagement - maven

I have maven-jaxb2-plugin. I generate jaxb objects and refer it in other classes of project.I've put jaxb plugin and compiler plugin under pluginManagement tag. Maven is executing compile phase first than generate phase where as if i remove pluginManagement tag, it works fine, first generate phase gets executed and all jaxb object gets generated and then compile phase gets executed. Due to pluginManagement tag, my project doesn't compile. Is pluginManagement tag used only for defining all the plugins in parent pom so that child pom can refer to these plugins ? My project is not a multi-module project.
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<schemaDirectory>${basedir}/src/main/resources/schema</schemaDirectory>
<generatePackage>com.common.dto</generatePackage>
<schemaIncludes>
<include>*.xsd</include>
</schemaIncludes>
<removeOldOutput>false</removeOldOutput>
<strict>false</strict>
<verbose>true</verbose>
<forceRegenerate>true</forceRegenerate>
<extension>true</extension>
</configuration>
</plugin>
</plugins>
</pluginManagement>

Yes, <pluginManagement> is used to create ready-to-use configurations, but does not automatically activate your plugins - you still need to include them.
So in effect you are right, <pluginManagement>, just like <dependencyManagement> are very useful in the parent pom to centralize plugin configurations and dependency management.
Effectively, 'declaring' your plugins in the right module benefits from a much more compact syntax:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
</plugin>
</plugins>

Related

How to exclude a dependency coming from parent in POM file

I am pulling in dependencies from a parent and need most of the dependencies in there.
But I also wish to be able to exclude 2 dependencies entirely. I am not able to edit the parent
thus this needs to be excluded from my POM file. Is this even possible? I've seen examples for overrides and quite a bit of suggestion to fix the parent POM which as mentioned, I can't do at this time.
Using Maven 3.3.x
My POM file
<parent>
<groupId>com.company.stuff</groupId>
<artifactId>our-parent</artifactId>
<version>1.7</version>
</parent>
<!-- other dependencies and build and plugins -->
The parent in above pulls in following plugins which I wish to exclude entirely.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>${some.version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>${some.version}</version>
</plugin>
Is there a way around this? Please advice. Thanks.
Tried with Thiago's suggestion, same outcome.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<version>${checkstyle.version}</version>
<executions>
<execution>
<id>maven-checkstyle-plugin</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
Why not just skip the plugin executions?
You could set the skip parameter of both plugins to true.
<plugin>
<groupId>group</groupId>
<artifactId>artifact</artifactId>
<executions>
<execution>
<id>ID_AS_IN_PARENT</id> <!-- id is necessary sometimes -->
<phase>none</phase>
</execution>
</executions>
</plugin>

run jasmine.js test as part of Maven 'test' goal

I have the following jasmine tests configuration in my project pom.xml:
<pluginManagement>
<plugins>
<plugin>
<groupId>com.github.searls</groupId>
<artifactId>jasmine-maven-plugin</artifactId>
<version>1.1.0</version>
<executions>
<execution>
<goals>
<goal>test</goal>
<goal>jasmine</goal>
</goals>
</execution>
</executions>
<configuration>
<jsSrcDir>${basedir}/src/main/js</jsSrcDir>
<sourceIncludes>
<include>myCode.js</include>
</sourceIncludes>
<jsTestSrcDir>${basedir}src/test/js/specs</jsTestSrcDir>
<specIncludes>
<include>mySpec.js</include>
</specIncludes>
</configuration>
</plugin>
...
with this configuraton I can run jsamine:test and the test wil run.
I want to run the tests as part of the goal test, but Maven won't run them.
I even tried removing the line <goal>jasmine</goal> but Maven still won't run the jasmine tests.
What am I doing wrong?
You must also add the execution of the plugin like this into you project:
<project>
<build>
<plugins>
<plugin>
<groupId>com.github.searls</groupId>
<artifactId>jasmine-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
cause the one you've defined in pluginManagement does not really execute the plugin.

How to Deploy only the sub-modules using maven deploy?

How do i deploy only the sub-modules of the project?
i have a project as;
ProjectA
- Submodule B
- Submodlue C
- Submodule D
The submodules are packaged as jar and is deployed to maven repo.how can only the sub -modules be deployed to the maven repository and not the main project?
Put this in module(s)(or module's pom.xml) that you don't want to deploy:
<properties>
<maven.deploy.skip>true</maven.deploy.skip>
</properties>
Since this is inherited by submodules, you have to put this in submodules that you do want to deploy:
<properties>
<maven.deploy.skip>false</maven.deploy.skip>
</properties>
Another suggestion could be doing the following:
mvn deploy -pl SubModuleB
This worked for me. Similar to other answer except added missing plugins element. Add to parent POM.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.4</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
You can use the technique described in my blog.
In this case, you'd disable default-deploy (or what the name is) in the root pom.xml:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<executions>
<execution>
<id>default-deploy</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
And then enable it for submodules:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<executions>
<execution>
<id>default-deploy</id>
<phase>deploy</phase>
</execution>
</executions>
</plugin>
This is working on my side
Put the plugin declaration in the parent pom , skip=true, but set inherited=false.
This prevents from repeating the on each child modules.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
<configuration>
<skip>true</skip>
</configuration>
<inherited>false</inherited>
</plugin>
You can configure the maven-deploy-plugin in the POM of a module to exclude it from the deploy:
<build>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.4</version>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
...
</build>

maven exclude plugin defined in parent pom

I have a parent pom with a few plugins. In my child pom, I want to exclude one plugin.
How can I do this?
I had a similar requirement to run some plugins in the child but not the parent POM. i achieved this by stating <skip>true</skip> in the parent POM.
the parent pom entry is below
<plugin>
<groupId>eviware</groupId>
<artifactId>maven-soapui-plugin</artifactId>
<version>4.0.0</version>
<inherited>false</inherited>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
</dependency>
</dependencies>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
The child project pom entry is below
<plugins>
<plugin>
<groupId>eviware</groupId>
<artifactId>maven-soapui-plugin</artifactId>
<version>4.0.0</version>
<configuration>
<settingsFile>site-service-web/src/test/soapui/soapui-settings.xml</settingsFile>
<projectFile>site-service-web/src/test/soapui/PodifiSite-soapui-project.xml</projectFile>
<outputFolder>site-service-web/target/surefire-reports</outputFolder>
<junitReport>true</junitReport>
<exportwAll>true</exportwAll>
<printReport>true</printReport>
</configuration>
</plugin>
</plugins>
You can define all plugins in parent pom in pluginManagement section
<pluginManagement>
<plugins>
...
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.8</version>
<executions>
<execution>
<id>unpack-dependencies</id>
<phase>generate-sources</phase>
<goals>
<goal>unpack-dependencies</goal>
</goals>
<configuration>
<includeTypes>tar.bz2</includeTypes>
<outputDirectory>${project.basedir}/target/dependencies</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
...
</plugins>
</pluginManagement>
And after in parent and child poms you can control the execution phase of this plugin
<plugins>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<inherited>false</inherited>
<executions>
<execution>
<id>unpack-dependencies</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
</plugins>
If you paste this in parent pom do not forget option <inherited>false</inherited>, its disable execution inheritance in child pom.
You can declare all the plugins in your parent pom within <pluginManagement>. In each child, you can declare the plugins which are used by that child. This way, you can include or exclude plugins as appropriate.
You can look at this related SO discussion as well.
You could use a profile activated by a property to determine whether the plugin is used or not. By default the property could be activated and the one project you do not want to use it in could include the property value to exclude the plugin.

Automatically activate parent plugin in Maven

Is it possible to have a plugin defined in the parent POM which is deactivated, and when the child inherits this plugin it gets automatically activated?
I guess you want to configure the plugin in your parent pom, but use it only in the inherited projects. Maven has a section for this - configure your plugins in pluginManagement, but bind them to a phase just when you needed it, e.g. omit the phase tag in pluginManagement, but specify it under in you inherited pom.
So 'siddhadev' is exactly correct. You can define the plugin configuration in the parent pom with a given id:
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>child-caller</id>
<!-- 'phase' omitted -->
<goals>
<goal>run</goal>
</goals>
<configuration>
<tasks>
<echo message="called from child!" />
</tasks>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
And, in the child POM, you can explicitly list the phase where this should be called:
<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<executions>
<execution>
<id>child-caller</id>
<phase>compile</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
I've used this for targeting various JREs. Unfortunately, because you can't use the maven-compiler-plugin with different destination directories (which I consider a bug in the plugin), you must use Ant.
This isn't exactly what you're after, but I think it will work well enough for you.
If you declare the plugin in a pluginManagement tag in the parent, the configuration will be inherited by any child projects that declare that plugin.
For example, in the parent declare that the compile plugin uses Java 5 for test compilation.
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>test-compile</id>
<goals>
<goal>testCompile</goal>
</goals>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
Then in a child, you simple declare the compiler plugin and the configuration from the parent will be inherited:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
</plugins>
</build>
You can declare a plugin at the top level pom and tell it to be skipped and then tell it to not be skipped at the child level. It's not quite automatic, but very minimal in the override verbosity.
Parent Pom, disabling the plugin, but declaring all the config:
<plugin>
<groupid>org.apache.maven.plugins</groupid>
<artifactid>maven-surefire-plugin</artifactid>
<configuration>
<skip>true</skip>
...lots more config...
...lots more config...
...lots more config...
</configuration>
</plugin>
Child Pom, enabling the plugin:
<plugin>
<groupid>org.apache.maven.plugins</groupid>
<artifactid>maven-surefire-plugin</artifactid>
<configuration>
<skip>false</skip>
</configuration>
</plugin>
I went with the following solution:
Configure the plugin in the parent-pom in the pluginManagement-section. Bind the plugin to an existing phase.
Deactivate the plugin for the parent-pom by binding it to a nonexistent phase: Override the phase in the plugins-section.
Activate the plugin in each child-pom by including the plugin in the plugins section.
Example parent-pom:
<defaultGoal>install</defaultGoal>
<pluginManagement>
<plugins>
...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<executions>
<execution>
<id>install-ejb-client</id>
<phase>install</phase>
<goals>
<goal>install-file</goal>
</goals>
<configuration>
<file>${ejb-client-file}</file>
<groupId>${project.groupId}</groupId>
<artifactId>${project.artifactId}</artifactId>
<version>${project.version}</version>
<packaging>jar</packaging>
<classifier>client</classifier>
</configuration>
</execution>
</executions>
</plugin>
...
</plugins>
</pluginManagement>
<plugins>
<plugin>
<!-- deactivate the plugin for this project, only child-projects do generate ejb-clients -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<inherited>false</inherited>
<executions>
<execution>
<id>install-ejb-client</id>
<phase>none</phase>
</execution>
</executions>
</plugin>
...
</plugins>
</build>
Example child-pom:
<build>
<plugins>
...
<plugin>
<!-- Install the generated client-jar. Property 'ejb-client-file' has to be set! Plugin configuration is in the parent pom -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
</plugin>
...
</plugins>
</build>
As far as I know, there is no generic solution for this. At least for the moment...
One idea (I didn't try it, but it may work) is to define, in the parent pom.xml an execution goal that does not exist, for example:
<executions>
<execution>
<goals>
<goal>noGoal</goal>
</goals>
</execution>
</executions>
and in every child, you redefine a correct goal.
The problem of this solution (if it works, of course ;) ) is that you must redefine the plugin configuration for every child. Otherwise, it will not be executed.

Resources