1

I currently have two projects:

api-test
    ...
    /config/config.json
    ...

and

ui-test
    ...
    /config/config.json
    ...

In eclipse, I am adding api-test in the build path of ui-test, so that api-test is the dependency of ui-test.

However the build failed, because api-test is looking for the config.json located in api-test/config/config.json by calling:

System.getProperty("user.dir") + "/config/config.json"

which does not exist in ui-test project.

the two config.json include different contents - what would be the best solution to let each project refer to their own config.json while ui-test is referring to api-test project?

jamesdeath123
  • 4,268
  • 11
  • 52
  • 93
  • Although it's not advisable, you can simply point to a file using its full path – Curcuma_ Sep 11 '15 at 21:46
  • Not that this would solve the path issue but why do you "_add api-test in the build path of ui-test_"? Why don't you add _api-test_ as dependency to the POM of _ui-test_ ? – Gerold Broser Sep 11 '15 at 23:34
  • If you mean to add the api-test.jar into POM of ui-test, I tried, but the jar does not contain the config.json so I sill will see the same error. Should jar file pack the json file? – jamesdeath123 Sep 15 '15 at 14:46
  • Leave the `json` files aside for a moment and get familiar with Maven's dependency concept(s) in [Introduction to the Dependency Mechanism](https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html) and [POM Reference, Dependencies](https://maven.apache.org/pom.html#Dependencies). You have a project (=library, =jar) `api-test`. Your other project `ui-test` depends on `api-test`, so the clean Maven way is to add `api-test` as dependency in `ui-test`'s POM. – Gerold Broser Sep 16 '15 at 09:59

1 Answers1

2

Put the files into the projects' src/main/resources directories as suggested by Maven's Standard Directory Layout. You can use relative paths to access these resources then.

See How to get file resource from Maven src/test/resources/ folder in JUnit test? For instance:

Test file existence

@Test
public void testStreamToString() {
  assertNotNull("Test file missing", getClass().getResource("/sample.txt"));
  ...
}
Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
  • If I move the config.json from api-test to within ui-test project, then ui-test project will end up with 2 config.json, which doesn't make sense? – jamesdeath123 Sep 15 '15 at 14:48
  • @jamesdeath123 Where did I write to do so? – Gerold Broser Sep 15 '15 at 15:24
  • You mentioned put the files into the projects' src/main/resources, and I assume what you mean is by put the api-test's config.json to the ui-test's src/main/resources folder? Please let me know if I am wrong. – jamesdeath123 Sep 15 '15 at 16:26
  • @jamesdeath123 That's not what I meant. I wrote "project**s'**" ... "director**ies**", i.e. put each file into `src/main/resources` of the project it belongs to. – Gerold Broser Sep 15 '15 at 16:46
  • I see! Unfortunately the config class that is reading that file is a singleton's static method, and cannot reference to the the non-static getClass()... – jamesdeath123 Sep 15 '15 at 17:47
  • @jamesdeath123 Are you able to work with a search engine of your choice? http://stackoverflow.com/questions/4391720/how-can-i-get-a-resource-content-from-a-static-context and http://stackoverflow.com/questions/8361947/how-to-get-getclass-getresource-from-a-static-context took me 5 seconds to find. – Gerold Broser Sep 16 '15 at 10:03