4

トークンのリストを含むファイルがあります。

tokens.txt

foo
bar
baz

およびテンプレート ファイル:

template.txt

public class @token@MyInterface implements MyInterface {
     public void doStuff() {
        // First line of generated code
        // Second line of generated code
     }
}

に次のソース コード ファイルを生成しますtarget/generated-sources/my/package

  • FooMyInterface.java
  • BarMyInterface.java
  • BazMyInterface.java

生成されたソース ファイルの 1 つは次のようになります。

FooMyInterface.java

public class FooMyInterface implements MyInterface {
     public void doStuff() {
        // First line of generated code
        // Second line of generated code
     }
}

Maven でこれを行うにはどうすればよいですか?

4

1 に答える 1

1

あなたがしようとしていることは、フィルタリングと呼ばれます。 詳細については、こちらをご覧ください。 ご覧のとおり、いくつかの方法を変更する必要があります。変数の定義は異なります。ファイルの名前を .java に変更します。

しかし、別の問題があります。これはソース ファイルを取得し、変数をリテラルに置き換えますが、プロジェクトをビルドするときに .java ファイルをコンパイルしません。あなたがそれをしたいと仮定すると、ここにその方法に関するチュートリアルがあります. いつか消える場合に備えて、そのチュートリアルの一部をインライン化します。

ソースファイルの例:

public static final String DOMAIN = "${pom.groupId}";
public static final String WCB_ID = "${pom.artifactId}";

フィルタリング:

<project...>
  ...
  <build>
    ...
    <!-- Configure the source files as resources to be filtered
      into a custom target directory -->
    <resources>
      <resource>
        <directory>src/main/java</directory>
        <filtering>true</filtering>
        <targetPath>../filtered-sources/java</targetPath>
      </resource>
      <resource>
        <directory>src/main/resources</directory>
        <filtering>true</filtering>
      </resource>
    </resources>
  ...
  </build>
...
</project>

ここで、maven がコンパイルするソース ファイルを見つけるディレクトリを変更します。

<project...>
  ...
  <build>
    ...
      <!-- Overrule the default pom source directory to match
            our generated sources so the compiler will pick them up -->
      <sourceDirectory>target/filtered-sources/java</sourceDirectory>
  ...
  </build>
...
</project>
于 2013-05-10T07:07:03.707 に答える