1

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?

Yves Nicolas
  • 6,901
  • 7
  • 25
  • 40

3 Answers3

1

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.

Amila
  • 5,195
  • 1
  • 27
  • 46
  • resource is added in the jar but not accessible thru the war – Yves Nicolas Sep 29 '15 at 17:33
  • In that case you can either write a public method in your jar to get resources, and call it from the war, or you can move your resources into the war file. – Amila Sep 29 '15 at 17:40
  • The method to get the jar resources was already public so not sure I understood well the first part of your comment and I did not want to copy the resources as it is reused in several modules. Maven remote resources plugin was the way to go, see my answer below. Your answer was usefull in my search however. – Yves Nicolas Oct 02 '15 at 10:30
1

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.

Community
  • 1
  • 1
Yves Nicolas
  • 6,901
  • 7
  • 25
  • 40
0

Use maven dependencies to add core project to your main project

   <dependency>
        <groupId>Group ID of Core Project</groupId>
        <artifactId>Artifact ID of Core Project</artifactId>
        <version>Core Project Verion</version>          
    </dependency>

Then create a method in Core project to read property from core - resources.Then return the values by that method.

So after adding Core project dependency to the main project you can access that method and read the properties in main project.

Chinthaka Dinadasa
  • 3,332
  • 2
  • 28
  • 33
  • it is already done this way but when the method is called from the war, it is not able to read the resources from the jar. Might be due to the use of SPring pathresolver? or by the fact that i am trying to read resources which are in a subdirectory from src/main/resources? – Yves Nicolas Sep 29 '15 at 17:35
  • Yes I am able to read it from Core. I was even being able to read it from WEBAPP inside Eclipse when doing some Junit tests. Problem appears only when I do the mvn clean install for deployment. Maven needs apparently to be explicitly told to copy resources from a dependency jar. Maven remote resources plugin was the way to make it work. – Yves Nicolas Oct 02 '15 at 10:33