Ste*_*han 5 java code-generation maven
我有一个包含令牌列表的文件:
令牌.txt
foo
bar
baz
Run Code Online (Sandbox Code Playgroud)
和一个模板文件:
模板.txt
public class @token@MyInterface implements MyInterface {
public void doStuff() {
// First line of generated code
// Second line of generated code
}
}
Run Code Online (Sandbox Code Playgroud)
我想生成以下源代码文件target/generated-sources/my/package
:
生成的源文件之一如下所示:
FooMyInterface.java
public class FooMyInterface implements MyInterface {
public void doStuff() {
// First line of generated code
// Second line of generated code
}
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能用 Maven 做到这一点?
您想要做的事情称为过滤。 你可以在这里读更多关于它的内容。 正如您所看到的,您必须改变做某些事情的方式。变量的定义不同。您需要将该文件重命名为 .java。
但是你又遇到了另一个问题:这将获取一个源文件并用文字替换变量,但当你构建项目时它不会为你编译 .java 文件。假设您想这样做,这里有一个关于如何操作的教程。 我将内联该教程的一些内容,以防有一天它消失:
示例源文件:
public static final String DOMAIN = "${pom.groupId}";
public static final String WCB_ID = "${pom.artifactId}";
Run Code Online (Sandbox Code Playgroud)
过滤:
<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>
Run Code Online (Sandbox Code Playgroud)
现在更改 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>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4367 次 |
最近记录: |