2013-08-18 16 views
0

我有一個Maven的奇怪問題。我正在使用過濾選項將某些版本信息放入屬性文件中。然後我將它包含在jar文件中,以便「Help/About」可以告訴我一些有用的信息。我遇到的問題是jar文件中的版本是以前的版本。因此,例如,如果我在0930運行構建,而在0940運行另一個構建,那麼在0940生成的jar中的屬性文件版本將具有0930的構建時間。我也使用buildNumber插件,但是此問題是否存在,無論我是否啓用它。Maven在jar中過濾的文件已過期

更奇怪的是,當我從eclipse中運行我的構建並運行程序時,「舊」文件顯示在幫助/ about上,但是當我「刷新」(F5)eclipse項目並重新運行程序,我會得到正確的版本。那麼maven是否會以某種方式取得日食版呢?爲什麼你需要在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>@[email protected]</token> 
          <value>${buildNumber}</value> 
         </replacement> 
         <replacement> 
          <token>@[email protected]</token> 
          <value>${maven.build.timestamp}</value> 
         </replacement> 
         <replacement> 
          <token>@[email protected]</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> 
+0

您知道「過濾」資源嗎?因爲您不需要替換插件來創建包含版本/編譯時間等的屬性文件。此外,[buildnumber-maven-plugin]的版本(http://mojo.codehaus.org/buildnumber-maven-plugin /)有點過時了。除此之外,你是如何稱呼你的構建? 'mvn install'? – khmarbaise

+0

我對maven很新穎,所以我不熟悉過濾。我更新了buildnumber插件,但正如我所說的那樣,這並不是真正的問題,因爲我在安裝它之前發現問題。我正在使用「包」目標。 –

+0

我已經添加了答案。但是Maven沒有「目標」,它有生命週期階段,有時你可以使用目標。 – khmarbaise

回答

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 構建時= $ {maven.build。時間戳}

而且做的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的替代品 - 插件,你不這樣簡單的場景需要的usuage。

+0

謝謝 - 我擺脫了替代品插件,它現在似乎一切正常。 –