Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven: use common/shared plugins when working with multiple profiles

Tags:

maven-2

I have a project that is using several profiles. Each profile uses following plugins:

  • maven-compiler-plugin
  • maven-resources-plugin
  • maven-antrun-plugin
  • maven-surefire-plugin
  • maven-war-plugin

The one marked in bold is however the only plugin where there is a difference between the profiles (different configuration files will be copied using the antrun plugin). The 4 other plugins are configured exactly the same for all profiles.

The question is now: is there some way to include these common plugins only once but still use them for all the profiles by default?

Something like:

<shared><plugin1><plugin2>...</shared>
<profile><plugin3></profile>
<profile><plugin3></profile>
...

thanks,
Stijn

like image 948
Stijn Geukens Avatar asked Feb 22 '10 15:02

Stijn Geukens


People also ask

Are Maven plugins inherited?

Default Configuration InheritanceIf the parent has a plugin, the child will automatically have it without additional configuration. This is like an inheritance. To achieve this, Maven merges the XML files at the element level.

What is the difference between plugin and pluginManagement tags?

pluginManagement: is an element that is seen along side plugins. Plugin Management contains plugin elements in much the same way, except that rather than configuring plugin information for this particular project build, it is intended to configure project builds that inherit from this one.

What are the two types of Maven plugins?

Introduction. In Maven, there are two kinds of plugins, build and reporting: Build plugins are executed during the build and configured in the <build/> element. Reporting plugins are executed during the site generation and configured in the <reporting/> element.


2 Answers

If a plugin is used by all profile, simply define it in the <build> part :

<project>
...
    <build>
        <plugins>
             Your shared plugins go here...
        </plugins>

    <profiles>
        Definition of profiles...
    </profiles>
</project>

This way, you will only define the antrun plugin in the profiles block.

like image 190
Romain Linsolas Avatar answered Oct 11 '22 22:10

Romain Linsolas


Just include the common plugins in your build section:

<build>
    <plugins>
        <plugin>
            <groupId>...</groupId>
            <artifactId>plugin1</artifactId>
        </plugin>
        ...
    </plugins>
</build>

Then add the specific plugin in your profile:

<profiles>
    <profile>
        <id>...</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>...</groupId>
                    <artifactId>plugin3</artifactId>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

You can also configure the same plugin differently in different profiles this way:

<profiles>
    <profile>
        <id>profile1</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>...</groupId>
                    <artifactId>plugin1</artifactId>
                    <configuration>
                        <setting>value1</setting>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
    <profile>
        <id>profile2</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>...</groupId>
                    <artifactId>plugin1</artifactId>
                    <configuration>
                        <setting>value2</setting>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>
like image 44
Péter Török Avatar answered Oct 11 '22 22:10

Péter Török