简体   繁体   中英

Copy file to jar root when building spring-boot project with Maven

I have a Spring Boot project and I'm building the jar file with mvn clean build. I need to copy a folder and a file to the root of the jar, where META-INF is located. I tried maven-resources-plugin but I can't reach my goal.

For war files I used maven-war-plugin in the past but I can't find something similar for jars.

Can anybody give me an idea?

Thanks.

I could manage to add files after the repackage goal of the spring-boot-maven-plugin using the maven-antrun-plugin and the Jar tool included in the JDK.

Updating a JAR File

The Jar tool provides au option which you can use to update the contents of an existing JAR file by modifying its manifest or by adding files.

The basic command for adding files has this format:

jar uf jar-file input-file(s)

https://docs.oracle.com/javase/tutorial/deployment/jar/update.html

Using Maven-Antrun-Plugin

This command can be executed using the maven-antrun-plugin together with the exec task, which allows you to execute commands via Runtime.exec(..) .

The entry may look like this:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>3.1.0</version>
    <executions>
        <execution>
            <phase>package</phase>
            <configuration>
                <target>
                    <exec executable="jar" vmlauncher="false">
                        <arg value="uf" />
                        <arg value="${project.build.directory}/myprogram.jar" />
                        <arg value="-C" />
                        <arg value="${project.build.directory}/classes" />
                        <arg value="org/company/app/Main.class" />
                    </exec>
                </target>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Notice the -C option, which allows you to change the directory that should not be included in the jar.

https://maven.apache.org/plugins/maven-antrun-plugin/usage.html

https://ant.apache.org/manual/Tasks/exec.html

Using Exec-Maven-Plugin

Alternatively the exec-maven-plugin can be used which does not require Ant to be installed.

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <executions>
        <execution>
            <phase>package</phase>
            <goals><goal>exec</goal></goals>
            <configuration>
                <executable>jar</executable>
                <arguments>
                    <argument>uf</argument>
                    <argument>${project.build.directory}/myprogram.jar</argument>
                    <argument>-C</argument>
                    <argument>${project.build.directory}/classes</argument>
                    <argument>org/company/app/Main.class</argument>
                </arguments>
            </configuration>
        </execution>
    </executions>
</plugin>

The final structure may look like this:

myprogram.jar
|-BOOT-INF/..
|-META-INF/..
|-org/springframework/boot/loader/..
|-org/company/app/Main.class

That way you can add any additional files you want after the jar has been packaged.

could something like the following help?

See also maven-jar-plugin

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.0.2</version>
            <configuration>
                <includes>
                    <include>**/cdi/*</include>
                    <include>**/META-INF/*</include>
                    <include>*.properties</include>
                </includes>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>test-jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

I resolved my problem by using the maven-assembly-plugin. I created an assembly zip which contains the application jar and some other resources. This solution is specific for applications which use AWS Elastic Beanstalk and need to implement the Procfile ( http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/java-se-platform.html#java-se-procfile ).

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