Maven Resources Do Not Copy Files

I want to use maven to replace some of my * .properties files in my war file.

So I created the dev, test and prod folders in my resources folder. Now I want one of these folders to be used in the resource folder path in my war file when executing the corresponding profile. As a result, maven also copies all other folders, so they have a double value in the class path. Here is my configuration:

<profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <build> <plugins> <plugin> <artifactId>maven-resources-plugin</artifactId> <version>2.3</version> <executions> <execution> <id>copy-dev-resources</id> <phase>process-resources</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <overwrite>true</overwrite> <outputDirectory>${basedir}/target/classes</outputDirectory> <resources> <resource> <!-- source --> <directory>src/main/resources/dev</directory> </resource> </resources> </configuration> </execution> </executions> </plugin> </plugins> </build> </profile> 

How to configure Maven now that it only copies the contents of the src / main / resources / dev folder to target / classes?

Thanks for helping

+7
source share
3 answers

Maven by default copies all the files in "src / main / resources" to the output folder, so in your current configuration it will probably create the folders "dev", "test" and "prod" with their contents, and then additionally copy the development resources without the prefix "dev".

I would suggest leaving the default resource folder as it is, and use it only for profile-independent resources. In your profile, you can configure additional resource folders:

 <profile> <id>dev</id> <build> <resources> <resource> <directory>${basedir}/src/main/resources-dev</directory> </resource> </resources> </build> </profile> 

It should also be possible to configure this in the general assembly section using the properties defined in the profiles:

 <build> <resources> <resource> <directory>${basedir}/src/main/resources-${projectStage}</directory> </resource> </resources> </build> <profiles> <profile> <id>dev</id> <properties> <projectStage>dev</projectStage> </properties> </profile> </profiles> 

If for some reason you cannot change your current directory structure, you will have to configure exclusions for the default resource folder so as not to copy the nested profile-dependent folders:

  <build> <resources> <resource> <directory>${basedir}/src/main/resources</directory> <excludes> <exclude>dev/**</exclude> <exclude>test/**</exclude> <exclude>prod/**</exclude> </excludes> </resource> </resources> </build> 
+7
source

For this purpose, there is a β€œmain” part in the maven directory structure. Instead of creating directories in the resource directory, you should create more dirs in the src directory. This is the default behavior, and it is already implemented, allowing test resources to override core resources during the maven lifecycle testing steps.

Your structure should look like this:

 src main resources a.properties b.properties c.properties test resources b.properties c.properties dev resources b.properties 

With this structure, your β€œprimary” target will have default property files, the test phases will be temporarily overwritten with b and c, and the dev profile will only overwrite b with another custom version in the final deployment.

To achieve this, you need to configure the maven-resources-plugin as you are already doing in our question for the dev profile.

And when you do this, also think: which version of the property files should be used when performing unit tests when building using the dev profile?

+1
source

I used the assembly.xml file in maven and used this set of code to make sure the file of my resources is in the root directory of the application ...

  <assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd"> <id>distribution</id> <formats> <format>tar.gz</format> </formats> <fileSets> <fileSet> <directory>${basedir}</directory> <includes> <include>bin/**</include> <include>webapps/**</include> <include>data/**</include> </includes> </fileSet> <fileSet> <directory>${basedir}/target</directory> <includes> <include>*.jar</include> </includes> <outputDirectory>lib</outputDirectory> </fileSet> <fileSet> <directory>${basedir}/src/main/</directory> <includes> <include>resources/**</include> </includes> <outputDirectory>/</outputDirectory> </fileSet> </fileSets> <dependencySets> <dependencySet> <outputDirectory>lib</outputDirectory> <useProjectArtifact>false</useProjectArtifact> </dependencySet> </dependencySets> </assembly> 

I don't know if this is the best way, but it was one that I could visualize.

0
source

All Articles