jar中的Maven过滤文件已过期

时间:2013-08-18 09:03:43

标签: eclipse maven

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

更奇怪的是,当我在eclipse中运行我的构建并运行程序时,“旧”文件显示在help / about上,但是当我“刷新”(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>

1 个答案:

答案 0 :(得分:0)

首先要激活对资源的过滤,如下所示:

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

    </resources>
    ...
  </build>

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

版本= $ {project.version}   buildNumber = $ {} buildNumber   构建时= $ {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的用法,这些插件在这种简单的场景中是不需要的。