Maven 3 profile with extensions

前端 未结 3 912
清酒与你
清酒与你 2020-12-16 18:55

My question had been addressed in this thread, but the explanation is not clear.

I have this build definition in one of my pom.xml files:



        
相关标签:
3条回答
  • 2020-12-16 19:10

    Indeed, the official Maven POM reference is not clear about the possible usage of extensions as part of a Maven profile, since it states you can have a build element within it, but not what of the build section.

    However, the official Maven model effectively filters and provides what of the build section you can actually use within a profile section. And indeed extensions is not there.

    However, what are Maven extensions? Build/Lifecycle enhancement, but also (and essentially): a library added to the runtime classpath of the Maven build, which participates to the build, but it is not packaged with the final artifact.

    Hence, in such a scenario (if you need to have extensions in profile or have a profile to change/add an extension) you could use the following trick:

    • Have an harmless extension as default extension of your build (where harmless means whatever library which could be part of your build classpath and essentially not affect it at all)
    • Have properties defining the GAV coordinates (GroupId, ArtifactId, Version) of this extension
    • Have a profile which overrides these properties with the desired (useful) extension

    As an example, given the following sample POM:

    <project>
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.sample</groupId>
        <artifactId>project</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    
        <properties>
            <extension.groupId>junit</extension.groupId>
            <extension.artifactId>junit</extension.artifactId>
            <extension.version>4.11</extension.version>
        </properties>
    
        <build>
            <extensions>
                <extension>
                    <groupId>${extension.groupId}</groupId>
                    <artifactId>${extension.artifactId}</artifactId>
                    <version>${extension.version}</version>
                </extension>
            </extensions>
        </build>
    
        <profiles>
            <profile>
                <id>customize-extension</id>
                <properties>
                    <extension.groupId>junit</extension.groupId>
                    <extension.artifactId>junit</extension.artifactId>
                    <extension.version>4.12</extension.version>
                </properties>
            </profile>
        </profiles>
    
    </project>
    

    The default build (without the customize-extension profile activated), would use the default defined properties and as such add junit as build extension: this is harmless (although it may create conflicts with another junit version of your build, so make sure you use the same version of use an even more harmless library for that).

    You can check Maven will pick it up by running a really first build phase, just to check information in our case, and enable the debug flag:

    mvn initialize -X
    

    And checking as part of the build log:

    [DEBUG] Populating class realm extension>junit:junit:4.11   
    [DEBUG]   Included: junit:junit:jar:4.11   
    

    Now let's use our trick: let's add (change) a build extension via profile:

    mvn initialize -X -Pcustomize-extension
    

    And as part of our build log we would have:

    [DEBUG] Populating class realm extension>junit:junit:4.12   
    [DEBUG]   Included: junit:junit:jar:4.12   
    

    Bingo. Maven picked up a different extension (in this case, a different version, the 4.12) and we succeeded on changing (or actually adding a meaningful) build extension via profile.

    0 讨论(0)
  • 2020-12-16 19:16

    Just a crazy idea: use modules

    Define a parent pom like this:

    <groupId>org.example</groupId>
    <artifactId>my-parent</artifactId>
    <version>1.0</version>
    <packaging>pom</packaging>
    <profiles>
        <profile>
            <id>use-pom1</id>
            <modules>
                <module>pom1</module>
            </modules>
        </profile>
        <profile>
            <id>use-pom2</id>
            <modules>
                <module>pom2</module>
            </modules>
        </profile>
    </profiles>
    

    Define the desired extensions on pom1 and pom2.

    0 讨论(0)
  • 2020-12-16 19:26

    I think the solution is here http://maven.apache.org/guides/mini/guide-using-extensions.html

    Define a build section where extensions are defined and then into the profile set the attribute true ( like in the second profile shown below )

    <build>
        <extensions>
            <extension>
                <groupId>org.apache.maven.wagon</groupId>
                <artifactId>wagon-ssh</artifactId>
                <version>2.9</version>
            </extension>
        </extensions>
    </build>
    
    <profiles>
        <profile>
            <id>create-default</id>
            <activation>
                <activeByDefault>true</activeByDefault>
                <property>
                    <name>build</name>
                    <value>full</value>
                </property>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-maven-plugin</artifactId>
                        <executions>
                            <execution>
                                <goals>
                                    <goal>repackage</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    
        <profile>
            <id>create-core</id>
            <activation>
                <property>
                    <name>build</name>
                    <value>full</value>
                </property>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-jar-plugin</artifactId>
                        <extensions>true</extensions>
                        <version>2.6</version>
                        <configuration>
                            <finalName>import-station-core-${project.version}</finalName>
                        </configuration>
                        <executions>
                            <execution>
                                <id>make-jar</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>jar</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
    
    0 讨论(0)
提交回复
热议问题