Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i include resources from a jar dependency project in a war file

Tags:

java

maven

A maven project WEBAPP is a war packaging. It depends on the CORE project (jar packaging) Inside WEBAPP, i want to access resources under the src/main/resource dir from CORE project. Mvn clean install doesnt seem to put these CORE resources in the war classpath.

What do i need to specify in the poms to do this inclusion?

like image 830
Yves Nicolas Avatar asked Oct 25 '25 00:10

Yves Nicolas


2 Answers

Maven does not include the jar dependency resources when building the war archive. The maven-remote-resources plugin is a way to achieve it. I was let to it by this SO question on copying resources

Just copy almost literally the example page of the plugin documentation. Resources I wanted to include in WEBAPP are located under src/main/resources/rulesproperties directory. In CORE pom.xml plugins section add :

   <plugin>
        <artifactId>maven-remote-resources-plugin</artifactId>
        <version>1.5</version>
        <executions>
          <execution>
            <goals>
              <goal>bundle</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <includes>
            <include>**/rulesproperties/*</include>
          </includes>
        </configuration>
      </plugin>

And in WEBAPP pom.xml plugin section, add

  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-remote-resources-plugin</artifactId>
    <version>1.5</version>
    <configuration>
      <resourceBundles>
        <resourceBundle>mvnGroupId:CORE:${project.version}</resourceBundle>
      </resourceBundles>
    </configuration>
    <executions>
      <execution>
        <goals>
          <goal>process</goal>
        </goals>
      </execution>
    </executions>
  </plugin> 

After a mvn clean install for the webapp project, the rulesproperties directory and its content is placed under the WEB-INF/CLASSES directory and correctly accessible as any other classpath resources.

like image 167
Yves Nicolas Avatar answered Oct 26 '25 15:10

Yves Nicolas


Resources added to src/main/resource directory will by included in the resulting jar by default.

You can access them using:

this.getClass().getResource("yourFile");

And

this.getClass().getResourceAsStream("yourFile");

You can verify whether your resources are included in the jar by opening/extracting it manually (using a zip utility). Your resources should be located at the root of the jar file.

like image 37
Amila Avatar answered Oct 26 '25 13:10

Amila



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!