Home  >  Maven

Change Maven Resource Directory

By Arvind Rai, May 31, 2021
We can change Maven default resource directory using <resource> element in our pom.xml file. The default resource directory is src/main/resources .

Change Resource Directory

Find the Maven code to change the default directory.
pom.xml
<project>
  ------
   <build>
    <resources>
	  <resource>
	     <directory>src/main/my-resources</directory>
	  </resource>
        ------
    </resources>
   </build>
</project> 




Create Many Resource Directories

We can create multiple resource directory using <resource> element multiple times within <resources> parent element.
pom.xml
<project>
  ------
   <build>
    <resources>
	  <resource>
	     <directory>src/main/my-resources1</directory>
	  </resource>
	  <resource>
	     <directory>src/main/my-resources2</directory>
	  </resource>
	  <resource>
	     <directory>src/main/my-resources3</directory>
	  </resource>	  
        ------
    </resources>
   </build>
</project> 

Create Maven Profile to Change Resource Directory

Here we will create two profiles prod and dev to change the default resource location. In our example, the dev profile will change to below resource location.
src/main/resources/dev 
The prod profile will change to below resource location.
src/main/resources/prod 
Find the Maven code.
pom.xml
<project>
  ------
	<profiles>
		<profile>
			<id>dev</id>
			<activation>
				<activeByDefault>true</activeByDefault>
			</activation>
			<build>
				<resources>
					<resource>
						<directory>src/main/resources/dev</directory>
					</resource>
				</resources>
			</build>
		</profile>
		<profile>
			<id>prod</id>
			<build>
				<resources>
					<resource>
						<directory>src/main/resources/prod</directory>
					</resource>
				</resources>
			</build>
		</profile>
	</profiles>
</project> 
The dev profile will be executed by default for the below command.
mvn clean install 
It is because the value of <activeByDefault> is true.
To execute prod profile, use below command.
mvn clean install -Pprod 

References

Specifying resource directories
Introduction to Build Profiles
POSTED BY
ARVIND RAI
ARVIND RAI











©2021 concretepage.com | Privacy Policy | Contact Us