I have java class files and property files in the same directory.
src/main/java/com/berlin/Test.class
src/main/java/com/berlin/Test.properties
With the maven jar I build and the maven target, I want these to appear in the same directory. but maven is placing the class files and property files in different places when I do 'mvn package'.
.. Output: jar -tvf file.jar:
Sat Jun 11 08:24:32 EDT 2011 main/java/com/berlin/Test.properties
I want: Sat Jun 11 08:24:32 EDT 2011 com/berlin/Test.properties Sat Jun 11 08:24:32 EDT 2011 com/berlin/Test.class
...
Part of my pom:
<build>
<finalName>${project.artifactId}</finalName>
<sourceDirectory>src</sourceDirectory>
<testSourceDirectory>test</testSourceDirectory>
<resources>
<resource>
<directory>src</directory>
<includes>
<include>**</include>
</includes>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.source.version}</source>
<target>${java.target.version}</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
For the Maven reasons, you should place properties file into src/main/resources, not in src/main/java directory (keeping the same subfolder structure).
That being said, to get what you want you need to replace (in your pom.xml) src
with src/main/java
. Not tested, but if you have problems please share.
First you should place your resources into src/main/resources/com/berlin/Test.properties and your java source files into src/main/java/com/berlin/Test.java...Never put compiled classes into src/ folder... furthermore you should remove the configuration:
<finalName>${project.artifactId}</finalName>
<sourceDirectory>src</sourceDirectory>
<testSourceDirectory>test</testSourceDirectory>
<resources>
<resource>
<directory>src</directory>
<includes>
<include>**</include>
</includes>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
from your pom, cause you don't need it and furthermore it's wrong (Convention over configuration paradigm.). Take a look at the default folder layout in maven.
The Maven Way is to put the source files (*.java) into src/main/java, the resources src/main/resources. The unit test classes src/test/java and src/test/resources after compiling it will be put into target/classes or for the tests target/test-classes.
<build>
<testResources>
<testResource>
<directory>src/test/resources</directory>
</testResource>
<testResource>
<directory>src/test/java</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</testResource>
</testResources>
</build>
Contrary to popular opinion, and flying in the face of what others consider as "the maven reasons":
Here are the magical pom.xml incantations that will accomplish this: (Assuming that your sources are in a directory called 'src')
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>src</directory>
<includes>
<include>**/*</include>
</includes>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
Beware that this will copy to the output folder every single file under 'src' which is not a java file. If you have more files that you do not want copied to the output folder, either add more exclusions, or replace '**/*' with a wildcard that only matches your resource files.
If you do the above, you can then have a class like the following, in the same package as the resources:
public class ResourceLocator
{
public static URL getResourceUrl( String name )
{
URL url = ResourceLocator.class.getResource( name );
assert url != null : name;
return url;
}
}
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.