Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从资源文件夹读取Java模板_Java_Maven 3 - Fatal编程技术网

从资源文件夹读取Java模板

从资源文件夹读取Java模板,java,maven-3,Java,Maven 3,我试图通过读取resources文件夹中的模板文件来动态创建pom.xml。我的模板文件如下所示。我读取这些文件,并用用户传递的参数替换groupId、artifactId和Version。当我尝试使用classloader groupId使用getResourceAsStream读取模板时,artificatId和Version被当前maven项目的值替换。我不知道为什么会这样。当我尝试从resources文件夹将其作为文件读取时,它会打印出准确的模板。我不知道为什么会发生替换。解决这个问题有

我试图通过读取resources文件夹中的模板文件来动态创建pom.xml。我的模板文件如下所示。我读取这些文件,并用用户传递的参数替换groupId、artifactId和Version。当我尝试使用classloader groupId使用getResourceAsStream读取模板时,artificatId和Version被当前maven项目的值替换。我不知道为什么会这样。当我尝试从resources文件夹将其作为文件读取时,它会打印出准确的模板。我不知道为什么会发生替换。解决这个问题有什么建议吗

代码 模板

4.0.0
${groupId}
${artifactId}
${version}
//Prints Correctly when read the file as below
File pomTemplateFile = new File("src/main/resources/templates/pom/pom.txt");
String oldPomTemplate = FileUtils.readFileToString(pomTemplateFile,StandardCharsets.UTF_8);
log.debug("POM Template :" + oldPomTemplate);
//Prints Incorrectly when i read it as resourcestream
InputStream is = WorkspaceManagementService.class.getResourceAsStream("/templates/pom/pom.txt");
String text =
        new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))
            .lines()
            .collect(Collectors.joining("\n"));
log.debug(" POM Template :" + text); 
 <project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven- 
    4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>${groupId}</groupId>
    <artifactId>${artifactId}</artifactId>
    <version>${version}</version>
</project>