Maven 目标将行附加到文件
Maven goal to append lines to file
上下文是在编译之前,有一个目标是下载一个 .proto (Google Protobuffer) 文件以在之后生成文件。在这个原型文件中没有提到包,所以当 类 生成时,它们没有出现在任何正确的包中,因此无法导入和使用。
此处的proto文件可视为简单的文本文件。我有一个非常具体的需求 - 我想在此文件的开头添加一些文本。
可以使用maven吗?
谢谢。
您可以使用nashorn-maven-plugin
<build>
<plugins>
<plugin>
<groupId>io.github.michaldo</groupId>
<artifactId>nashorn-maven-plugin</artifactId>
<version>0.0.1</version>
<executions>
<execution>
<phase>process-resources</phase>
<configuration>
<script>
var Files = Java.type('java.nio.file.Files');
var Paths = Java.type('java.nio.file.Paths');
var content = new java.lang.String(Files.readAllBytes(Paths.get($basedir+ '/a.txt')));
content = "From nashorn with love\n" + content;
Files.write(Paths.get($basedir + '/a.txt'),
content.getBytes());
</script>
</configuration>
<goals>
<goal>eval</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
上下文是在编译之前,有一个目标是下载一个 .proto (Google Protobuffer) 文件以在之后生成文件。在这个原型文件中没有提到包,所以当 类 生成时,它们没有出现在任何正确的包中,因此无法导入和使用。
此处的proto文件可视为简单的文本文件。我有一个非常具体的需求 - 我想在此文件的开头添加一些文本。
可以使用maven吗?
谢谢。
您可以使用nashorn-maven-plugin
<build>
<plugins>
<plugin>
<groupId>io.github.michaldo</groupId>
<artifactId>nashorn-maven-plugin</artifactId>
<version>0.0.1</version>
<executions>
<execution>
<phase>process-resources</phase>
<configuration>
<script>
var Files = Java.type('java.nio.file.Files');
var Paths = Java.type('java.nio.file.Paths');
var content = new java.lang.String(Files.readAllBytes(Paths.get($basedir+ '/a.txt')));
content = "From nashorn with love\n" + content;
Files.write(Paths.get($basedir + '/a.txt'),
content.getBytes());
</script>
</configuration>
<goals>
<goal>eval</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>