0

I created a simple plugin in maven which aims to generate files automatically. The files are created by reading templates that are located, inside the plugin, in the resources folder (src / main / resources). When I use the plugin outside of an application it works correctly. If I try to run the plugin within an exisisting project, it cannot find the template files. How can I get around?

I copy resource in the jar in this way

<resources>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.txt</include>
            </includes>
        </resource>
</resources>

I use this code to read file

ClassLoader classLoader = getClass().getClassLoader();
String x = classLoader.getResource("template/" + fileName).getFile();
File file = new File(x);    

The value of x is correct:

file:\C:\Users\myname\.m2\repository\com\ciro\myapp\0.0.1-SNAPSHOT\myapp-0.0.1-SNAPSHOT.jar!\template\assembler.txt

but when I execute

new File(x) 

I get

file:\C:\Users\myname.m2\repository\com\ciro\myapp\0.0.1-SNAPSHOT\myapp-0.0.1-SNAPSHOT.jar!\template\

The syntax of the file, directory, or volume name is incorrect

ciro
  • 771
  • 1
  • 8
  • 30

1 Answers1

0

I solved in this way

InputStream in = getClass().getResourceAsStream("/template/" + fileName);
BufferedReader reader = new BufferedReader(new InputStreamReader(in));

String line = reader.readLine();

while (line!=null){
    data.add(line);
    line= reader.readLine();
}

following this link

Read all lines with BufferedReader

ciro
  • 771
  • 1
  • 8
  • 30