Eclipse m2e将覆盖buildnumber-maven-plugin中的buildNumber

时间:2016-01-20 19:28:40

标签: eclipse maven m2e buildnumber-maven-plugin

我正在使用Eclipse m2e,buildnumber-maven-plugin和templating-maven-plugin来创建一个带有$ {buildNumber}的过滤java文件。

以下是src / main / java-templates / build.java文件中的示例:

public static final String BUILDNUMBER = "${buildNumber}";

如果我运行" mvn generate-sources"在命令行上,我的过滤的java文件包含生成的buildNumber值。如果我运行" generate-sources"在使用m2e的Eclipse中,过滤的java文件简要包含实际的buildNumber,但很快就被原始字符串" $ {buildNumber}"

替换

项目的结构是

CmbProduct/pom.xml
+-- Model/pom.xml
+-- other modules

buildnumber-maven-plugin在父pom.xml中运行,因为我使用buildNumber为所有子模块设置一般<finalName>值。

我已经尝试过org.eclipse.m2e的生成周期映射和没有生命周期映射。 我没有发现任何变化。

我有几个&#34; echo&#34;用于调试目的的两个pom.xml文件中的任务。这些在运行时显示正确/实际的buildNumber。

这是缩写的CmbProduct / pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

    <groupId>com.magnicomp</groupId>
    <artifactId>Product</artifactId>
    <!-- Do not change version EVER. This is used by MagniCompCommon and all other sub modules. -->
    <version>1.0</version>
    <packaging>pom</packaging>

    <name>CMB Product</name>

    <modules>

        <module>Model</module>
        <module>Common</module>

    </modules>

    <scm>
        <!-- SCM used by buildnumber-maven-plugin and others -->
        <connection>scm:git:git://sol:/vol/git/cmb.git</connection>
    </scm>

    <properties>
        <!-- 
            The canonical Product Version is defined below.  This value is
            automatically propagated to Java via plugin in the Modelproject.

            The ${buildNumber} is automatically generated on-the-fly by
            buildnumber-maven-plugin
        -->
        <!-- 
            Product version defined primary version of product.
            This is used instead of project.version because the later must
            remain the same. 
        -->
        <product.version.base>1.0.0</product.version.base>
        <product.version.status>alpha1</product.version.status>
        <product.version>${product.version.base}.${buildNumber}-${product.version.status}</product.version>
        <!-- Version in 4 dot (digits + dot) format suitable for Windows file Version -->
        <product.version.4dot>${product.version.base}.${buildNumber}</product.version.4dot>

        <!-- Java version -->
        <project.source.version>1.8</project.source.version>
        <project.target.version>1.8</project.target.version>

        <!-- MagniComp common -->
        <failOnMissingWebXml>false</failOnMissingWebXml>
        <hibernate.version>4.3.11.Final</hibernate.version> <!-- was 4.3.10.Final -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <texo.version>0.9.0-v201501182340</texo.version>
        <emf.version>2.11.0-v20150123-0347</emf.version>

        <!-- Vaadin -->
        <vaadin.version>7.5.7</vaadin.version>
        <vaadin.plugin.version>${vaadin.version}</vaadin.plugin.version>
        <vaadin.icons.version>1.0.1</vaadin.icons.version>

        <!-- Product specific -->
        <javax.servlet.version>3.1</javax.servlet.version>
        <jersey.glassfish.version>2.21</jersey.glassfish.version>
        <bouncy.version>1.51</bouncy.version>
        <build.helper.maven.plugin.version>1.9.1</build.helper.maven.plugin.version>
        <maven.shade.plugin.version>2.4.1</maven.shade.plugin.version>
        <jna.version>4.1.0</jna.version>
    </properties>

    <build>
        <finalName>${project.artifactId}-${product.version}</finalName>

        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>2.7</version>
                    <configuration>
                        <encoding>${project.encoding}</encoding>
                    </configuration>
                </plugin>

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>2.6</version>
                </plugin>

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <version>2.6</version>
                </plugin>

                <plugin>
                  <artifactId>maven-assembly-plugin</artifactId>
                  <version>2.6</version>
                  <!-- We do not need a default assembly.xml 
                  <configuration>
                    <descriptors>
                      <descriptor>src/main/assembly/bin.xml</descriptor>
                    </descriptors>
                  </configuration>
                   -->
                </plugin>

                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>exec-maven-plugin</artifactId>
                    <version>1.4.0</version>
                </plugin>

                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>templating-maven-plugin</artifactId>
                    <version>1.0.0</version>
                </plugin>

                <plugin>
                    <groupId>org.eclipse.m2e</groupId>
                    <artifactId>lifecycle-mapping</artifactId>
                    <version>1.0.0</version>
                    <configuration>
                        <lifecycleMappingMetadata>
                            <pluginExecutions>
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>org.codehaus.mojo</groupId>
                                        <artifactId>buildnumber-maven-plugin</artifactId>
                                        <versionRange>[1.4,)</versionRange>
                                        <goals>
                                            <goal>create</goal>
                                            <goal>create-metadata</goal>
                                        </goals>
                                    </pluginExecutionFilter>
                                    <action>
                                        <execute>
                                            <runOnIncremental>true</runOnIncremental>
                                        </execute>
                                    </action>
                                </pluginExecution>
                            </pluginExecutions>
                        </lifecycleMappingMetadata>
                    </configuration>
                </plugin>

            </plugins>
        </pluginManagement>

        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <encoding>${project.encoding}</encoding>
                    <source>${project.source.version}</source>
                    <target>${project.target.version}</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>buildnumber-maven-plugin</artifactId>
                <version>1.4</version> <!-- Keep in sync with pluginsManagement -->
                <inherited>false</inherited> <!-- Run only for this level project -->
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>create</goal> <!-- Create buildNumber -->
                            <goal>create-metadata</goal> <!-- Create build.properties -->
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <!-- 
                    Auto update incremental integer as build number. 
                    Without this it uses git revision. 
                    -->
                    <format>{0,number,integer}</format>
                    <items>
                        <item>buildNumber</item>
                    </items>
                    <!-- Store buildNumber in given file.  Use product.version so it resets each time version changes -->
                    <buildNumberPropertiesFileLocation>${basedir}/buildinfo/buildNumber-${product.version.base}-${product.version.status}.properties</buildNumberPropertiesFileLocation>
                    <!-- WORKAROUND: Make ${buildNumber} available to child modules -->
                    <getRevisionOnlyOnce>true</getRevisionOnlyOnce>
                </configuration>
            </plugin>       

            <plugin> <!-- Help identify buildNumber -->
                <artifactId>maven-antrun-plugin</artifactId>
                <inherited>false</inherited> <!-- Run only for this level project -->
                <executions>
                    <execution>
                        <phase>validate</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <tasks>
                                <echo>Build Number (buildNumber) is ${buildNumber}</echo>
                            </tasks>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <!-- This plugin allows us to add "src-gen" as another source dir -->
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>${build.helper.maven.plugin.version}</version>
                <executions>
                    <execution>
                        <id>add-source</id>
                        <phase>generate-sources</phase>
                        <goals><goal>add-source</goal></goals>
                        <configuration>
                            <sources>
                                <source>src-gen</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.18.1</version>
                <configuration>
                  <suiteXmlFiles>
                    <suiteXmlFile>src/test/resources/testng-unit.xml</suiteXmlFile>
                  </suiteXmlFiles>
                </configuration>
            </plugin>

        </plugins>
    </build>
    ... snip ....

这是Model / pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.magnicomp</groupId>
        <artifactId>Product</artifactId>
        <version>1.0</version>
    </parent>

    <artifactId>Model</artifactId>
    <packaging>jar</packaging>

    <build>
        <resources> <!-- XXX ARE WE GOING TO USE THIS? -->
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>version.properties</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>

        <pluginManagement>
            <plugins>
                <!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
                <!-- Override eclipse error about "create-metadata" goal from above plugin -->
                <plugin>
                    <groupId>org.eclipse.m2e</groupId>
                    <artifactId>lifecycle-mapping</artifactId>
                    <version>1.0.0</version>
                    <configuration>
                        <lifecycleMappingMetadata>
                            <pluginExecutions>
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>org.codehaus.mojo</groupId>
                                        <artifactId>templating-maven-plugin</artifactId>
                                        <versionRange>[1.0.0,)</versionRange>
                                        <goals>
                                            <goal>filter-sources</goal>
                                        </goals>
                                    </pluginExecutionFilter>
                                    <action>
                                        <execute>
                                            <runOnIncremental>true</runOnIncremental>
                                        </execute>
                                    </action>
                                </pluginExecution>
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>org.codehaus.mojo</groupId>
                                        <artifactId>buildnumber-maven-plugin</artifactId>
                                        <versionRange>[1.4,)</versionRange>
                                        <goals>
                                            <goal>create</goal>
                                            <goal>create-metadata</goal>
                                        </goals>
                                    </pluginExecutionFilter>
                                    <action>
                                        <execute>
                                            <runOnIncremental>true</runOnIncremental>
                                        </execute>
                                    </action>
                                </pluginExecution>
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>org.apache.maven.plugins</groupId>
                                        <artifactId>maven-antrun-plugin</artifactId>
                                        <versionRange>[1.3,)</versionRange>
                                        <goals>
                                            <goal>run</goal>
                                        </goals>
                                    </pluginExecutionFilter>
                                    <action>
                                        <ignore></ignore>
                                    </action>
                                </pluginExecution>
                            </pluginExecutions>
                        </lifecycleMappingMetadata>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>

        <plugins>

            <plugin> <!-- Aid with making sure buildNumber is available -->
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <id>echo-buildnumber-1</id>
                        <phase>validate</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <tasks>
                                <echo>Build number during validate is ${buildNumber}</echo>
                            </tasks>
                        </configuration>
                    </execution>
                    <execution>
                        <id>echo-buildnumber-2</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <tasks>
                                <echo>Build number during generate-sources is ${buildNumber}</echo>
                            </tasks>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin> <!-- Must be after buildnumber-maven-plugin due to ${buildNumber} -->
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>templating-maven-plugin</artifactId>
                <executions>
                    <!-- Take files in sourceDirectory and filter them to outputDirectory -->
                    <execution>
                        <id>filter-src</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>filter-sources</goal>
                        </goals>
                        <configuration>
                            <sourceDirectory>${basedir}/src/main/java-templates</sourceDirectory>
                            <outputDirectory>${basedir}/src-generated</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <!-- Copy the build.properties created by buildnumber-maven-plugin -->
                        <id>copy-build-properties</id>
                        <phase>validate</phase>
                        <goals> <goal>copy-resources</goal> </goals>
                        <configuration>
                            <outputDirectory>${basedir}/src/main/resources</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>${project.parent.basedir}/target/generated/build-metadata</directory>
                                    <filtering>true</filtering>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

        </plugins>


    </build>
    ... snip ...

1 个答案:

答案 0 :(得分:0)

它已经有一段时间了,但我使用timestampFormat和timestampPropertyName配置属性创建了时间戳目标,并在Eclipse m2e生命周期映射中添加了runOnConfiguration true。

内部版本号插件:

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>buildnumber-maven-plugin</artifactId>
    <version>1.4</version>
    <configuration>
        <revisionOnScmFailure>just.say.no.scm.config.in.pom</revisionOnScmFailure>
    </configuration>
    <executions>
        <execution>
            <phase>validate</phase>
            <goals>
                <goal>create-timestamp</goal>
            </goals>
            <configuration>
                <timestampFormat>yyyyMMddHHmmssS</timestampFormat>
                <timestampPropertyName>myBuildNumberVariable</timestampPropertyName>
                <!-- formats the timestamp all together like 20160404141705123 and puts 
                    it in the ${myBuildNumberVariable} buildProperty -->
            </configuration>
        </execution>
    </executions>
</plugin>
相关问题