Paralife
Paralife

Reputation: 6236

In maven how can I include non-java src files in the same place in the output jar?

I received a source code bundle. Inside the src directory tree there are some properties files(.properties) which I want to keep in the output jar in the same place. e.g: I want

src/main/java/com.mycompany/utils/Myclass.java 
src/main/java/com.mycompany/utils/Myclass.properties

to stay the same in the jar:

com.mycompany/utils/Myclass.class 
com.mycompany/utils/Myclass.properties

without needing to add the properties file it to separate resources folder. Is there a way to I tell this to maven?

Upvotes: 55

Views: 38217

Answers (3)

GaRzY
GaRzY

Reputation: 85

Include and mix all your non .java src files and the src/main/resources:

<resources>
        <resource>
            <directory>src/main/resources</directory>
        </resource>
        <resource>
            <directory>${project.build.sourceDirectory}</directory>
            <excludes>
                <exclude>**/*.java</exclude>
            </excludes>
        </resource>
    </resources>

    <testResources>
        <testResource>
            <directory>src/test/resources</directory>
        </testResource>
        <testResource>
            <directory>${project.build.testSourceDirectory}</directory>
            <excludes>
                <exclude>**/*.java</exclude>
            </excludes>
        </testResource>
    </testResources>

Upvotes: 2

Raghuram
Raghuram

Reputation: 52665

You could add the following in your pom indicating that the resources are available in src/main/java and including the type of resources.

<build>
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.properties</include>
            </includes>
        </resource>
    </resources>
</build>

Upvotes: 76

Erik van Oosten
Erik van Oosten

Reputation: 1738

With this pom fragment you include anything that is not a java file for both main and test artifact:

<build>
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <excludes>
                <exclude>**/*.java</exclude>
            </excludes>
        </resource>
    </resources>
    <testResources>
        <testResource>
            <directory>src/test/java</directory>
            <excludes>
                <exclude>**/*.java</exclude>
            </excludes>
        </testResource>
    </testResources>
</build>

Upvotes: 14

Related Questions