18

テキストファイルを含むMaven Webアプリケーションがあります

src/main/webapp/textfilesdir

私が理解しているように、パッケージ段階で、この textfilesdir ディレクトリは

ターゲット/プロジェクト-1.0-SNAPSHOT

ディレクトリに圧縮されます。

ターゲット/プロジェクト-1.0-SNAPSHOT.war

問題

ここで、target/project-1.0-SNAPSHOT/textfilesdir にあるテキスト ファイルの内容を文字列置換する必要があります。これは、textfilesdir が target/project-1.0-SNAPSHOT にコピーされた後、target/project-1.0-SNAPSHOT.war ファイルが作成される前に行う必要があります。これはすべてパッケージ段階で行われると思います。

プラグイン (場合によっては maven-antrun-plugin) をパッケージ フェーズにプラグインして、これを行うにはどうすればよいでしょうか。

テキスト ファイルには、フィルタリングする ${property-name} などのプロパティは含まれていません。文字列の置換が唯一のオプションである可能性があります。

オプション

  1. target/project-1.0-SNAPSHOT ディレクトリにコピーした後、WAR を作成する前に、テキスト ファイルを変更します。

  2. パッケージ化後、WAR からテキスト ファイルを抽出し、変更して、WAR に追加し直します。

ここには、私が見逃している別のオプションがあると考えています。誰か考えますか?

4

5 に答える 5

8

私は同じ問題を抱えていて、この問題をかなりいじっていたので、この質問はかなり古いですが答えます。leandro と Phil が述べたように、maven-replacer-plugin を使用できます。しかし、彼らの解決策は私にはうまくいきませんでした。有効useCacheにするとエラーが発生し、プロジェクトをビルドできなくなりました。さらに、自動クリーニングを適切に機能させることができません。投稿にコメントすることはまだ許可されていないため、ここで完全なソリューションを提供します。

まず、maven-replacer-plugin を設定します。

<plugin> 
  <groupId>com.google.code.maven-replacer-plugin</groupId>
  <artifactId>maven-replacer-plugin</artifactId>
  <version>1.3.7</version>
  <executions>
    <execution>
      <phase>prepare-package</phase>
      <goals>
        <goal>replace</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <includes>
      <include>target/${project.build.finalName}/static/**/*.css</include>
    </includes>
    <regex>false</regex>
    <token>someString</token>
    <value>replaceString</value>
  </configuration>
</plugin>

実際の戦争が構築される前に、爆発した戦争を作成します。これは、war ファイルの内容全体がサブディレクトリ (デフォルトでは target/${project.build.finalName}) に保存されることを意味します。その後、maven-replacer-plugin は、指定したとおりにファイルの内容を変更します。default-war最後に、.war はジョブによってパッケージ フェーズにパックされます。展開された war フォルダーの内容が上書きされないようにするには、展開さwarSourceDirectoryれた war が保存されているディレクトリに設定する必要があります。次の構成スニペットは、このジョブを実行します。

<plugin>
  <artifactId>maven-war-plugin</artifactId>
  <version>2.1.1</version>
  <executions>
    <execution>
      <id>prepare</id>
      <phase>prepare-package</phase>
      <goals>
        <goal>exploded</goal>
  </goals>
    </execution>
    <execution> 
      <id>default-war</id>
      <phase>package</phase>
      <goals>
        <goal>war</goal>
      </goals>
      <configuration>
        <warSourceDirectory>${project.build.directory}/${project.build.finalName}</warSourceDirectory>
      </configuration>
    </execution>
  </executions>
</plugin>

置き換えられたコンテンツを含むパッケージは、次を使用して構築できます mvn clean package

于 2012-09-28T13:30:40.450 に答える
5

オプション 1 は実行できません。prepare-packagepackageすぎて遅すぎます。そのため、カスタム作業をプラグインできる場所がわかりません。オプション 2 は実行可能ですが、痛みを伴う IMO です。そこで、さらにいくつかの命題を示します (すべて AntRun とReplaceRegExpおよび/またはReplaceタスクに基づいています)。

解決策 1:

  1. 処理する必要があるテキスト ファイルを配置する新しいフォルダーを作成します。
  2. antrun プラグインを にバインドしprepare-package、ファイルを処理するように設定し、処理されたファイルをtarget(例: target/textfilesdir) の下のディレクトリに置きます。
  3. target/textfilesdirとして含めるように war プラグインを構成しますwebResource。詳細については、外部 Web リソースの追加とフィルタリングを参照してください。

解決策 2:

  1. antrun プラグインを にバインドしprepare-package、テキスト ファイルを処理するように設定しsrc/main/webapp/textfilesdir、処理されたファイルをtarget/project-1.0-SNAPSHOT.
  2. 以前に処理されたファイルを除外するように war プラグインを構成します。詳細については、外部 Web リソースの追加とフィルタリングを参照してください。

私は2番目の解決策に行くと思います。

于 2010-06-10T21:05:31.480 に答える
3

ファイルをwarにコピーせずにprepare-packageを使用して、フェーズに問題がありました。私の解決策は次のとおりです。

この構成を maven-war-plugin に追加します

<plugin>
             <groupId>org.apache.maven.plugins</groupId> 
             <artifactId>maven-war-plugin</artifactId> 
              <version>2.1.1</version> 
         <executions>
          <execution>
               <phase>prepare-package</phase>
               <goals>
                    <goal>exploded</goal>
               </goals>
          </execution>
          <executions> 
               <configuration>
               <webResources>
                    <resource>
                         <directory>${basedir}/WebContent?</directory> <excludes>
               <!--Exclude the file because it is copied using the maven replacer plugin -->
                         <exclude>/style.scss</exclude>
                         </excludes>
                    </resource>
               </webResources>
          </configuration>
          </plugin>
          Add the configuration to replacer 
 <plugin>
       <groupId>com.google.code.maven-replacer-plugin</groupId> 
       <artifactId>replacer</artifactId> 
       <version>1.5.1</version> 
       <executions>
          <execution>
               <id>scss-replacement</id> <phase>prepare-package</phase> <goals>
               <goal>replace</goal>
               </goals> <configuration>
               <file>WebContent?/css/style.scss</file>
               <!-- My local file(WebContent?/css/style.scss) have a param
               $url: "http://localhost:8080/interface";
               --> <!-- regex with match to $url: "http://localhost:8080/interface"; -->
               <token>\$url:.</token>
               <!-- Replace to -->
               <value>\$url: "www.myapplication.com.br/css/style.css";</value>
               <outputFile>target/app/css/style.scss</outputFile>

               </configuration>
          </execution>
     <executions>
<plugin>

出力ファイルはファイルを上書きしないので、構成 maven-war-plugin を配置して、ファイル style.scss を除外します。さよなら!

于 2012-11-08T12:30:20.943 に答える
2

私からの別の解決策です。パーティーに遅れて申し訳ありませんが、それでも一部の人にとっては役立つかもしれません。オーバーレイが配置されていたため、メインソースで置換プラグインを使用するのが困難だったため、上記のソリューションは単に機能しませんでした。したがって、maven-war-plugin を使用してパッケージの準備フェーズで一時ディレクトリを生成し、それを操作してそのディレクトリから war を解放するための代替プラグインを使用して、他に何も上書きされないようにしました。

      <plugin>
    <artifactId>maven-war-plugin</artifactId>
    <executions>
      <execution>
        <id>prepare</id>
        <phase>prepare-package</phase>
        <goals>
          <goal>exploded</goal>
        </goals>
        <configuration>
          <webappDirectory>${project.build.directory}/${project.build.finalName}-patched/</webappDirectory>
        </configuration>
      </execution>
      <execution>
        <id>default-war</id>
        <phase>package</phase>
        <goals>
          <goal>war</goal>
        </goals>
        <configuration>
          <useCache>true</useCache>
          <warSourceDirectory>${project.build.directory}/${project.build.finalName}-patched/</warSourceDirectory>
        </configuration>
      </execution>
    </executions>
  </plugin>
  <plugin>
    <groupId>com.google.code.maven-replacer-plugin</groupId>
    <artifactId>replacer</artifactId>
    <executions>
      <execution>
        <phase>prepare-package</phase>
        <goals>
          <goal>replace</goal>
        </goals>
      </execution>
    </executions>
    <configuration>
      <file>${project.build.directory}/${project.build.finalName}-patched/fileToBeChanged.txt</file>
      <token>ValueToChange</token>
      <value>ValueToReplace</value>
    </configuration>
  </plugin>
于 2016-10-13T11:16:03.240 に答える
1

maven-replacer-plugin を使用できます。

<plugin>
       <groupId>com.google.code.maven-replacer-plugin</groupId>
       <artifactId>maven-replacer-plugin</artifactId>
       <version>1.3.7</version>
       <executions>
           <execution>
               <phase>prepare-package</phase>
               <goals>
                   <goal>replace</goal>
               </goals>                   
           </execution>
       </executions>
       <configuration>
           <file>target/${project.artifactId}-${project.version}/WEB-IN/site.xml</file>
           <replacements>
               <replacement>
                   <token>ear.version</token>
                   <value>${ear.version}-${maven.build.timestamp}</value>
               </replacement>         
           </replacements>
       </configuration>
   </plugin>

ただし、あと 2 つのトリックが必要です。1 つは、Explore ゴールを war プラグインに追加することです。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.1.1</version>
        .....
        <executions>
        <execution>
        <phase>prepare-package</phase>
            <goals>
                 <goal>exploded</goal>
             </goals>
         </execution>
    </executions>
</plugin>

最後に、パッケージの前に mvn clean を呼び出す必要があります。あなたのpomからそれを行うことができます:

                   <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>2.4.1</version>
                    <executions>
                      <execution>
                        <id>auto-clean</id>
                        <phase>initialize</phase>
                        <goals>
                          <goal>clean</goal>
                        </goals>
                      </execution>
                    </executions>
            </plugin>

2.0.1 より新しいバージョンの maven-war-plugin を使用している場合は、maven-war-plugin の構成に true を含める必要があります。そうしないと、ファイルへの変更がwar プラグインは、webapp を 2 回以上コピーします。Hudson/Jenkins を使用している場合は、2.0.1 より新しいバージョンを使用する必要があります。

于 2011-04-04T16:02:34.490 に答える