Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making maven copy additional files inside the build jar (not resources but any file inside any package)?

I have a package org.myapp.mypackage with some ruby files (*.rb) and I need to include them in the generated build jar in the same package along with the java class files.

How do I tell my friend Maven to do this?

Note : No, I cannot copy to anywhere else, but thanks for suggesting. :)

like image 387
chrisapotek Avatar asked May 01 '12 04:05

chrisapotek


People also ask

How do I package a Maven project into a jar?

In order to compile the project into an executable jar, please run Maven with mvn clean package command.

How do I copy files from one directory to another in Maven?

Using the Copy Rename Maven Plugin The copy-rename-maven-plugin helps copying files or renaming files/directories during the Maven build lifecycle. Upon building the project, we'll see foo. txt in the target/destination-folder.

What is JAR file in Maven?

jar'. The resulting 'jar' file contains the compiled java class files as well as the files from src/main/resources . Usually there is no need to mentioned the 'maven-jar-plugin' explicit cause it's bound to the Maven Build Life Cycle. For full documentation, click here.


2 Answers

You can modify the resources section of the <build> bit of the POM:

<resources>
  <resource>
    <directory>src/main/resources</directory>
  </resource>
  <resource>
    <filtering>false</filtering>
    <directory>src/main/java</directory>
    <includes>
      <include>*.rb</include>
    </includes>
    <excludes>
      <exclude>**/*.java</exclude>
    </excludes>
  </resource>
</resources>

Or, the other answer (create the same package structure in src/main/resources) will also work.

like image 71
artbristol Avatar answered Oct 29 '22 04:10

artbristol


Not sure if i understood the problem correctly, but if your Ruby files are packaged by maven and declared as a dependency, you can use the shade plugin to include the contents into the resulting jar file:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>1.4</version>
    <executions>
        <execution>
            <phase>package</phase>
            <goals>
                <goal>shade</goal>
            </goals>
            <configuration>
                <artifactSet>
                    <includes>
                        <include>org.myapp.mypackage:mypackage</include>
                    </includes>
                </artifactSet>
                <filters>
                    <filter>
                        <artifact>org.myapp.mypackage:mypackage</artifact>
                        <includes>
                            <include>org/my/package/*.rb</include>
                        </includes>
                    </filter>
                </filters>
            </configuration>
        </execution>
    </executions>
</plugin>
like image 31
Jörn Horstmann Avatar answered Oct 29 '22 04:10

Jörn Horstmann