Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a file under resources folder to a zip using Maven

Tags:

java

maven

I have a zip.xml in a folder named assembly:

<assembly
        xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3
        http://maven.apache.org/xsd/assembly-1.1.3.xsd">
    <id>archive</id>
    <baseDirectory>/</baseDirectory>
    <formats>
        <format>zip</format>
    </formats>
    <fileSets>
        <fileSet>
            <directory>${project.build.directory}</directory>
        </fileSet>
        <fileSet>
            <directory>src/main/resources/</directory>
            <includes>
                <include>plugin-descriptor.properties</include>
            </includes>
        </fileSet>
    </fileSets>
</assembly>

After I build the project using mvn clean -U -DskipTests package assembly:assembly, I notice that only the jar file was copied and zipped to the archive.

How can I make the properties file to archived in the zip file? The wanted zip should have the jar and properties file in root

like image 770
Animesh Pandey Avatar asked Oct 20 '25 12:10

Animesh Pandey


1 Answers

You should define a <files> element instead of a <fileSets> since you want to copy specific individual files and not a directory. In this case, you want to copy:

  • The final JAR, so this is ${project.build.directory}/${project.build.finalName}.${project.packaging} into the root of the ZIP
  • The properties file src/main/resources/plugin-descriptor.properties into the root of the ZIP

Your current configuration is wrong because you are declaring to ZIP all entries under ${project.build.directory}, which is not what you want, since you're only interested in the final JAR. Also, using / as base directory is a bit weird; it would be better to say that we don't want a base directory with <includeBaseDirectory>false</includeBaseDirectory>

A sample configuration would be:

<assembly
    xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.3
        http://maven.apache.org/xsd/assembly-1.1.3.xsd">
    <id>archive</id>
    <includeBaseDirectory>false</includeBaseDirectory>
    <formats>
        <format>zip</format>
    </formats>
    <files>
        <file>
            <source>${project.build.directory}/${project.build.finalName}.${project.packaging}</source>
            <outputDirectory>/</outputDirectory>
        </file>
        <file>
            <source>${basedir}/src/main/resources/plugin-descriptor.properties</source>
            <outputDirectory>/</outputDirectory>
        </file>
    </files>
</assembly>
like image 53
Tunaki Avatar answered Oct 23 '25 03:10

Tunaki