1
votes

I have pretty standard Maven build following default lifecycle phases. However, at the beginning I need to generate to target directory JAR file with just one particular resource and then continue the build. How can I do it using standard plugins?

At http://maven.apache.org/plugins/maven-jar-plugin/plugin-info.html I see only possiblity to generate JAR for entire project.

1
Make a separate module which contains the special folder and that's it...No special configurations etc... - khmarbaise
What kind of resources? Can you show use your pom file? - khmarbaise
@khmarbaise it's just one XML file that I want to have as separate JAR. Tunaki already provided working answer, thanks - Michal Kordas
What i don't understand is where is this XML file coming from? Does it exist in the same module? And why do you need a JAR which contains only a single file? For what purpose ? This setup sounds wrong... - khmarbaise
@khmarbaise This is XML with a schema used by my app that I need to distribute as JAR. Yes, it exists in the same module. What's wrong with that? And yes, at some point I will create a separate module for that, but now I needed it just for playing around. - Michal Kordas

1 Answers

2
votes

You can use the maven-jar-plugin for this task by specifying a classesDirectory:

Directory containing the classes and resource files that should be packaged into the JAR.

By default, this points to ${project.build.outputDirectory} in order to make a JAR of the classes and resources of your entire project.

Within this folder, you can then select what specific resource you want to include with the includes parameter. An example of configuration would be the following:

<plugin>
    <artifactId>maven-jar-plugin</artifactId>
    <version>3.0.0</version>
    <executions>
        <execution>
            <id>init-jar</id>
            <phase>initialize</phase>
            <goals>
                <goal>jar</goal>
            </goals>
            <configuration>
                <classesDirectory>${basedir}/path/to/folder</classesDirectory>
                <includes>
                    <include>resource.txt</include>
                </includes>
                <outputDirectory>${project.build.directory}</outputDirectory>
                <finalName>resource</finalName>
            </configuration>
        </execution>
    </executions>
</plugin>

This will make a JAR out of the file ${basedir}/path/to/folder/resource.txt inside the build folder. This JAR will be named resource.jar, as per the configured finalName. The execution is bound to the initialize phase since you want to create this JAR at the beginning of the build.