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

JarMaven

Jar Problem Overview


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?

Jar Solutions


Solution 1 - Jar

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>

Solution 2 - Jar

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>

Solution 3 - Jar

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>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionParalifeView Question on Stackoverflow
Solution 1 - JarRaghuramView Answer on Stackoverflow
Solution 2 - JarErik van OostenView Answer on Stackoverflow
Solution 3 - JarGaRzYView Answer on Stackoverflow