0

我对maven有一个奇怪的问题。我正在使用过滤选项将一些版本信息放入属性文件中。然后我将它包含在 jar 文件中,以便“帮助/关于”可以告诉我一些有用的信息。我遇到的问题是jar文件中的版本是以前的版本。因此,例如,如果我在 0930 运行构建,在 0940 运行另一个构建,则在 0940 生成的 jar 中的属性文件版本的构建时间将为 0930。我也在使用 buildNumber 插件,但是这个无论我是否启用它,都存在问题。

更奇怪的是,当我从 Eclipse 中运行构建并运行程序时,“旧”文件显示在帮助/关于,但是当我“刷新”(F5)Eclipse 项目并重新运行程序时,我得到了正确的版本。那么maven会以某种方式采用eclipse版本吗?以及为什么需要在 Eclipse 中“刷新”以使其拥有最新版本。

无论如何,我的 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>

    <scm>
        <url>scm:git:https://github.com/gregryork/DayOneViewer</url>
        <developerConnection>scm:git:https://github.com/gregryork/DayOneViewer</developerConnection>

        <tag>master</tag>
    </scm>


    <groupId>uk.co.gregreynolds</groupId>
    <artifactId>dayone</artifactId>
    <version>0.0.2-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>dayone</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <version.template.file>src/main/resources/uk/co/gregreynolds/dayone/Version.properties.template</version.template.file>
        <version.file>src/main/resources/uk/co/gregreynolds/dayone/Version.properties</version.file>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>buildnumber-maven-plugin</artifactId>
                <version>1.0</version>
                <executions>
                    <execution>
                        <phase>generate-resources</phase>
                        <goals>
                            <goal>create</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <doCheck>false</doCheck>
                    <doUpdate>false</doUpdate>
                    <revisionOnScmFailure>true</revisionOnScmFailure>
                    <format>{0,date,yyyy-MM-dd_HH-mm}_{1}</format>
                    <items>
                        <item>timestamp</item>
                        <item>${user.name}</item>
                    </items>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.0</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <version>2.1</version>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                        <configuration>
                            <transformers>
                                <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                    <mainClass>uk.co.gregreynolds.dayone.DayOneViewer</mainClass>
                                </transformer>
                            </transformers>
                        </configuration>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>com.google.code.maven-replacer-plugin</groupId>
                <artifactId>maven-replacer-plugin</artifactId>
                <version>1.4.0</version>
                <executions>
                    <execution>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>replace</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <file>${version.template.file}</file>
                    <outputFile>${version.file}</outputFile>
                    <replacements>
                        <replacement>
                            <token>@buildnumber@</token>
                            <value>${buildNumber}</value>
                        </replacement>
                        <replacement>
                            <token>@buildtime@</token>
                            <value>${maven.build.timestamp}</value>
                        </replacement>
                        <replacement>
                            <token>@pomversion@</token>
                            <value>${project.version}</value>
                        </replacement>
                    </replacements>
                </configuration>
            </plugin>

        </plugins>

        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-site-plugin</artifactId>
                    <version>3.3</version>
                    <dependencies>
                        <dependency><!-- add support for ssh/scp -->
                            <groupId>org.apache.maven.wagon</groupId>
                            <artifactId>wagon-ssh</artifactId>
                            <version>1.0</version>
                        </dependency>
                    </dependencies>
                </plugin>
                <!--This plugin's configuration is used to store Eclipse m2e settings 
                    only. It has no influence on the Maven build itself. -->
                <plugin>
                    <groupId>org.eclipse.m2e</groupId>
                    <artifactId>lifecycle-mapping</artifactId>
                    <version>1.0.0</version>
                    <configuration>
                        <lifecycleMappingMetadata>
                            <pluginExecutions>
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>
                                            com.google.code.maven-replacer-plugin
                                        </groupId>
                                        <artifactId>
                                            maven-replacer-plugin
                                        </artifactId>
                                        <versionRange>
                                            [1.4.0,)
                                        </versionRange>
                                        <goals>
                                            <goal>replace</goal>
                                        </goals>
                                    </pluginExecutionFilter>
                                    <action>
                                        <ignore></ignore>
                                    </action>
                                </pluginExecution>
                            </pluginExecutions>
                        </lifecycleMappingMetadata>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.googlecode.plist</groupId>
            <artifactId>dd-plist</artifactId>
            <version>1.3</version>
        </dependency>
        <dependency>
            <groupId>org.swinglabs</groupId>
            <artifactId>swingx</artifactId>
            <version>1.6.1</version>
        </dependency>
    </dependencies>

    <distributionManagement>
        <site>
            <id>langurmonkey.no-ip.org</id>
            <url>scp://langurmonkey.no-ip.org/var/www/dayone/</url>
        </site>
    </distributionManagement>
</project>
4

1 回答 1

0

第一件事是像这样激活您的资源过滤:

  <build>
    <resources>
      <resource>
        <directory>src/main/resources/</directory>
        <filtering>true</filtering>
      </resource>

    </resources>
    ...
  </build>

这将激活对 . 文件夹(和子文件夹)中所有文件的过滤src/main/resources。有时您只需要对有限数量的文件进行过滤。在这种情况下,只需将这些文件放入子文件夹并适当地更改上述配置即可。接下来是定义一个属性文件(在src/main/resources文件夹中),其中包含您需要的所有信息,如下所示:

版本=${project.version} buildNumber=${buildNumber} buildTime=${maven.build.timestamp}

此外,像这样对buildnumber-maven-plugin进行配置:

 <build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>buildnumber-maven-plugin</artifactId>
            <version>1.2</version>
            <executions>
              <execution>
                <phase>validate</phase>
                <goals>
                  <goal>create</goal>
                </goals>
             </execution>
           </executions>
            <configuration>
                <doCheck>false</doCheck>
                <doUpdate>false</doUpdate>
                <revisionOnScmFailure>UNKNOWN</revisionOnScmFailure>
                <format>{0,date,yyyy-MM-dd_HH-mm}_{1}</format>
                <items>
                    <item>timestamp</item>
                    <item>${user.name}</item>
                </items>
            </configuration>
        </plugin>

之后,您可以简单地删除在如此简单的场景中不需要的 maven-replacer-plugin 的使用。

于 2013-08-18T13:56:09.157 回答