2017-04-21 21 views
2

カスタムMavenプラグインでMavenビルドタイムスタンプにアクセスしたいとします。したがって、私は特別な変数maven.build.timestampを使用しようとします:

maven.build.timestampビルドの開始を示すタイムスタンプ。 Parameter#defaultValueとMavenの2.1.0-M1

ため:

パラメータのデフォルト値、最終的に注入時に解釈される${...}式を含む:PluginParameterExpressionEvaluatorを参照します。

しかし、私はいつも値nullを取得します。私はタイプDateとタイプStringで試しました。

Javaコード:

@Mojo(name = "test", defaultPhase = LifecyclePhase.GENERATE_SOURCES) 
public class TestMojo extends AbstractMojo { 

    @Parameter(defaultValue = "${maven.build.timestamp}", readonly = true) 
    private Date timestampDate; 

    @Parameter(defaultValue = "${maven.build.timestamp}", readonly = true) 
    private String timestampString; 

    @Override 
    public void execute() throws MojoExecutionException, MojoFailureException { 
     getLog().error("timestampDate: " + timestampDate); 
     getLog().error("timestampString: " + timestampString); 
    } 
} 

プラグインの設定:

<plugin> 
    <groupId>com.mycompany</groupId> 
    <artifactId>test-maven-plugin</artifactId> 
    <version>0.0.12</version> 
</plugin> 

ログイン:

[INFO] Scanning for projects... 
[INFO]                   
[INFO] ------------------------------------------------------------------------ 
[INFO] Building test 0.0.1 
[INFO] ------------------------------------------------------------------------ 
[INFO] 
[INFO] --- test-maven-plugin:0.0.12:test (default-cli) @ test    --- 
[ERROR] timestampDate: null 
[ERROR] timestampString: null 
[INFO] ------------------------------------------------------------------------ 
[INFO] BUILD SUCCESS 
[INFO] ------------------------------------------------------------------------ 
[INFO] Total time: 0.799 s 
[INFO] Finished at: 2017-04-21T14:37:20+02:00 
[INFO] Final Memory: 8M/223M 
[INFO] ------------------------------------------------------------------------ 

環境:8

    • JavaはなぜカスタムのMavenプラグインに特殊変数に解決されていない

    のMaven 3.3.3を組み込み?

  • 答えて

    1

    2つの回避策が見つかりました。

    @Mojo(name = "test", defaultPhase = LifecyclePhase.GENERATE_SOURCES) 
    public class TestMojo extends AbstractMojo { 
    
        @Parameter 
        private String timestamp; 
    
        @Override 
        public void execute() throws MojoExecutionException, MojoFailureException { 
         getLog().error("timestamp: " + timestamp); 
        } 
    } 
    

    プラグイン構成:

    <plugin> 
        <groupId>com.mycompany</groupId> 
        <artifactId>test-maven-plugin</artifactId> 
        <version>0.0.12</version> 
        <configuration> 
         <timestampString>${maven.build.timestamp}</timestampString> 
        </configuration> 
    </plugin> 
    

    プラグイン構成

    Javaコードで

    • maven.build.timestamp欠点:

      構成のボイラープレートコード。デフォルト値として

    • session.request.startTime

      Javaコード:

      @Mojo(name = "test", defaultPhase = LifecyclePhase.GENERATE_SOURCES) 
      public class TestMojo extends AbstractMojo { 
      
          @Parameter(defaultValue = "${session.request.startTime}", readonly = true) 
          private Date timestamp; 
      
          @Override 
          public void execute() throws MojoExecutionException, MojoFailureException { 
           getLog().error("timestamp: " + timestamp); 
          } 
      } 
      

      プラグイン構成:

      <plugin> 
          <groupId>com.mycompany</groupId> 
          <artifactId>test-maven-plugin</artifactId> 
          <version>0.0.12</version> 
      </plugin> 
      

      欠点:

      session.request.startTimeの値は常にmaven.build.timestampと同じであることはわかりません。 maven.build.timestamp.formatで定義された形式は自動的には使用されません。

    関連する問題