Java – how do I enable Maven profiles when the build version is not – snapshot?

I'm trying to use the gitflow helper Maven plugin extension for my Maven build

Therefore, I want to configure my project to run some additional steps when building the release version and skip them when compiling the snapshot version, but if ${project. Version} contains - snapshot, I can't find a way to enable the configuration file

Any suggestions?

Solution

The following is a possible method. You can always simulate the if statement in Maven build:

>Use the buy helper Maven plugin and its regex property target to resolve the default ${project. Version} attribute and create a new ${only. When. Is. Snapshot. Used} attribute with a value of true or ${. Find the project. Version} of the snapshot suffix. > Use its skipsource option to configure Maven source plugin to execute its jar target with special configuration, and pass it a new (dynamic) ${only. When. Is. Snapshot. Used} attribute: if it is a snapshot, it will skip the execution of valuetruth, otherwise it will have a ${project. Version} value, which will be used as false, Therefore, this execution will not be skipped > use its skip option to configure the same as above for maven Javadoc plugin

An example of the above method is:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.10</version>
    <executions>
        <execution>
            <!-- sets the only.when.is.snapshot.used property to true if SNAPSHOT was used,to the project version otherwise -->
            <id>build-helper-regex-is-snapshot-used</id>
            <phase>validate</phase>
            <goals>
                <goal>regex-property</goal>
            </goals>
            <configuration>
                <name>only.when.is.snapshot.used</name>
                <value>${project.version}</value>
                <regex>.*-SNAPSHOT</regex>
                <replacement>true</replacement>
                <failIfNoMatch>false</failIfNoMatch>
            </configuration>
        </execution>
    </executions>
</plugin>

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-source-plugin</artifactId>
    <version>3.0.1</version>
    <executions>
        <execution>
            <id>create-sources</id>
            <phase>package</phase>
            <goals>
                <goal>jar</goal>
            </goals>
            <configuration>
                <!-- skip when version is SNAPSHOT -->
                <skipSource>${only.when.is.snapshot.used}</skipSource>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-javadoc-plugin</artifactId>
    <version>2.10.4</version>
    <executions>
        <execution>
            <id>create-javadoc</id>
            <phase>package</phase>
            <goals>
                <goal>jar</goal>
            </goals>
            <configuration>
                <!-- skip when version is SNAPSHOT -->
                <skip>${only.when.is.snapshot.used}</skip>
            </configuration>
        </execution>
    </executions>
</plugin>

That is, no configuration file is required, this configuration will be enabled only if a non snapshot version will be used, dynamic and without any further configuration (command line options or any other)

As a reminder, you can also check the Maven release plugin, which will effectively call the source and Javadoc plug-ins. Only when performing is released, there is no additional (secondary) complexity of the above methods

Otherwise, you can simply use the default configuration file from Maven super POM, which will actually call the same source and Javadoc plug-in, and can be activated by setting the property performrelease to the value true That is, in any Maven project, you can call the following:

mvn clean package -DperformRelease=true

or

mvn clean package -Prelease-profile

And you will automatically benefit from the default super configuration file and generate source and Javadoc jars, although this method should be used as the last option, because the configuration file can be deleted from the super POM in future versions

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>