0

I have 4 projects: A,B,C And Common.

A,B,C uses both some java files and some XSD/XML files in Common. A,B,C shouldn't know about each other, but only Common. I need to find the Common files(both java an non-java) in each project when testing for example. How do i link them so i can find the files in eg. A when writing code accessing files?

Here is the POM from A and Common:

 Common:
 <groupId>dk.myCompany.sa</groupId>
 <artifactId>common</artifactId>
 <version>0.0.3-SNAPSHOT</version>
 <packaging>jar</packaging>
 <name>common</name>
 <url>http://maven.apache.org</url>

 A
 <groupId>dk.myCompany.sa</groupId>
 <artifactId>A</artifactId>
 <version>0.0.2-SNAPSHOT</version>
 <packaging>jar</packaging>
 <dependencies>
  <dependency>
   <groupId>dk.myCompany.sa</groupId>
   <artifactId>common</artifactId>
   <version>0.0.3-SNAPSHOT</version>
  </dependency>
 </dependencies>
Sofus Albertsen
  • 163
  • 1
  • 1
  • 11

2 Answers2

1

Any files in the src/main/resources folder of Common will be on the classpath of A, B and C if you add Common as a dependency. So as long as you load your files using getResourceAsStream (see How to really read text file from classpath in Java), they will be accessible.

Community
  • 1
  • 1
artbristol
  • 32,010
  • 5
  • 70
  • 103
  • The question you relate to are talking about setting classpath beforehand to fetch the file. I don't want to have to set the classpath to something everytime i deploy this to someplace. i was hoping for a relative path like when i locate files inside the project eg: "./src/test/java/dk/myCompany/dataretrival/test/testdata/initalConfig.xml" – Sofus Albertsen Mar 07 '12 at 14:36
  • You don't have to set the classpath, Maven does it for you. Just make sure you use `getResourceAsStream `. – artbristol Mar 07 '12 at 14:41
  • Ok having tried this in two dummy projects i can see that you don't have to set classpath. Now i just need to figure out why the classpath won't be set correct in my real project. Thanks! – Sofus Albertsen Mar 07 '12 at 15:54
0

Define A, B and C as modules of Common's pom:

-- common.pom
<groupId>dk.myCompany.sa</groupId>
<artifactId>common</artifactId>
<version>0.0.3-SNAPSHOT</version>
<packaging>jar</packaging>
<name>common</name>
<modules>
    <module>A</module>
    <module>B</module>
    <module>C</module>
</modules>

You will probably also need to define manifest files for A,B and C which reference the current common jar on their classpath.

mcfinnigan
  • 11,442
  • 35
  • 28