Maven: How can my mojo access its own resources?

I have a project (here called my-artifact) that should generate sources from a model file. I created maven-plugin (my-code-generator), which is used as described in the pom.xml excerpt below. It loads and processes model.xml from my-artifact resources and generates code using some predefined templates stored in the plugin. The question is how my code generator could access these templates, because they are not in the project resources, but within its own resources.

Thanks in advance

<plugin>
  <group_id> my-group </ group_id>
        <artifact> my generator code </ artifact>
        <& version of GT; 0.0.1-SNAPPER </ & GT version;
        <configuration GT;
                <ModelFile>
                        src / main / resources / model.xml
                </modelDir>
        </ configurations>
        < shots> <
                execution>
                        <phase> generate-sources </ phases>
                        <targets and GT;
                                <& target GT; generate-model </ target>
                        </ goals>
                </ fulfillment>
        <& / shots GT;
</ plugin>
<& plugin GT;
        <group_identifier> org.codehaus.mojo </group_id>
        <artifact> built-in helper-Maven plugin & lt; / artifact>
        <footshot>
                <execution>
                        <ID> adding a source </ & ID GT;
                        <phase> generate-sources </phase>
                        <goals and GT;
                                <& target GT; add source </ & target GT;
                                <sources>
                                        <source>target / generated-sources </source>
                                <& / GT sources;
                        </ configuration>
                </ execution>
        <& / shots GT;
</ plugin>

+5
source share
2 answers

Just use ClassLoader to get resources from the MyCodeGenerator Maven plugin.

Add something like this to your MyCodeGeneratorMojo

    URL getTemplate(String fileName) {
        return this.getClass().getResource(fileName);
    }

In the MyCodeGenerator Maven plugin, add the template to the directory src/main/resources(do not forget to use the correct package entry (s) in this directory).

+5
source

By including them in the jar file for the plugin and referring to them through the class path, through ClassLoader.getResourceAsStream.

, , API- , .

+3

All Articles