3

I have a multi-project gradle build containing 3 projects: A, B and C.

A and B depends on C.
C has a file myconfig.xml in its resources folder that I would like to include as resource in war generated for A and B.

A -- project type - WAR
  -- depends on - C
B -- project type - WAR
  -- depends on - C
C -- project type - JAVA
\_ src\main\resources
 \_ myconfig.xml

However, simply having C as a dependency for A & B doesnt seem to do that. The myconfig.xml file is not present in the war file's WBE-INF\classes folder. It is indeed present in C.jar file but that is not where its needed.

How do i share the resources folder of C such that its in the war file of A and B?

pdeva
  • 43,605
  • 46
  • 133
  • 171
  • Any sample project online to have a try? – Opal May 27 '15 at 07:56
  • 1
    Consider accessing the xml directly from `C.jar`, since it is in class path. You can open the resource as stream. Check this post http://stackoverflow.com/questions/20389255/reading-a-resource-file-from-within-jar – Anton Krosnev Jun 03 '15 at 11:52
  • @AntonK. can you add this as an answer – pdeva Jun 04 '15 at 08:58

4 Answers4

3

I am not sure how it works in gradle but in maven you could do it with maven-dependency-plugin:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-dependency-plugin</artifactId>
        <configuration>
            <artifactItems>
                <artifactItem>
                    <groupId>yourgroupid</groupId>
                    <artifactId>C</artifactId>
                    <version>${project.version}</version>
                    <type>jar</type>
                    <overWrite>true</overWrite>
                    <outputDirectory>${project.build.directory}/WEB-INF/classes</outputDirectory>
                    <includes>myconfig.xml</includes>
                </artifactItem>
            </artifactItems>
        </configuration>
        <executions>
            <execution>
                <phase>compile</phase>
                <goals>
                    <goal>unpack</goal>
                </goals>
            </execution>
        </executions>
    </plugin>

There should be something similar in gradle too, just a pointer maybe.

Leon
  • 1,141
  • 1
  • 10
  • 25
2

Consider accessing the xml directly from C.jar, since it is in class path. You can open the resource as stream. Check this post Reading a resource file from within jar

Community
  • 1
  • 1
Anton Krosnev
  • 3,964
  • 1
  • 21
  • 37
1

Can you explain how do you read myconfig.xml file? If you try using getClass().getResourceAsStream("/myconfig.xml") you will read it no matter it's in WEB-INF/classes folder or in a jar in classpath. Obviously, is better to put resources in jars into folders (e.g: my-c-lib/myconfig.xml) in order to avoid collisions.

sinuhepop
  • 20,010
  • 17
  • 72
  • 107
-1

To use the resource of another project, we should import that project to our class path.

To import use, < classpathentry combineaccessrules="false" kind="src" path="/your project c path"/>

Add this entry in the classpath(.classpath file) project A and B

dilly
  • 67
  • 4
  • we are talking about a gradle project here. not sure which .classpath file you are talking about? – pdeva May 29 '15 at 04:25
  • i am not familiar with gradle , but there must be a way to add dependency project at the time of compilation. hoping this will be use full: https://java.net/downloads/satjug/Enter%20the%20Gradle.pdf – dilly May 29 '15 at 05:51