简体   繁体   中英

Unable to retrieve resource in custom maven plugin during compile phase

I have created a custom maven plugin which runs in compile phase. The plugin attempts to find a resource on the class path, but can not find it. The plugin:

<plugin>
    <groupId>com.mygroup</groupId>
    <artifactId>myplugin-maven-plugin</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <executions>
        <execution>
            <phase>compile</phase>
                <goals>
                    <goal>build</goal>
                </goals>
        </execution>
    </executions>
</plugin>

In the plugin's code I am using:

InputStream is = this.getClass().getClassLoader().getResource("myFile.txt").openStream();

Which results in a NullPointerException when executing mvn clean install, having tried to put myFile.txt in the project's root, src folder or src/main/resources folder. Every time getResource returns null. I also tried using:

InputStream is = Thread.currentThread().getContextClassLoader().getResource("myFile.txt").openStream();

And also both with getResourceAsStream but nothing helps.

Does this have something to do with the compile phase? I would like to have the plugin generate code based on properties in the input file.

Thanks!

I found a solution. First I thought the problem went away when not specifying the phase, but when not doing that, it will not execute the plugin at all. What did the trick for me is simply use FileInputStream("myFile.txt") having myFile.txt in the project root.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM