Michal Kordas
Michal Kordas

Reputation: 10925

Create JAR with one file in Maven

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.

Upvotes: 1

Views: 2082

Answers (1)

Tunaki
Tunaki

Reputation: 137064

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.

Upvotes: 2

Related Questions