Guifan Li
Guifan Li

Reputation: 1653

Dependencies path difference in Gradle vs in Maven

I have a question regarding to the difference between Gradle depedencies mechanism vs Maven dependency mechanism:

My project structure is following and app is dependent on common:

project
    common 
         conf
         src
            java
            test
    app 
         conf
         src
            java 
            test

and build.gradle in app is:

dependencies {
    compile project(':common')
    ....
}
sourceSets {
    main {
        java {
            srcDir 'src/java'
        }
        resources {
            srcDir 'conf'
        }
    }

    test {
        java {
            srcDir 'src/test'
        }
    }
}

When I use ant dist. The class paths contain /common/conf folder, which contains lots of configuration files. When I use Gradle build. The class paths contain build/common.jar instead of /common/conf.

Is there a way I could make Gradle do the same thing as Maven does (make class paths contain /common/conf instead of build/common.jar)?

Because app will read xml configuration files under common/conf folder when I run test cases under app but app is not able to read xml from a jar file. Because right now my code is not able to handle the inputStream from Jar.

Is it using:

res.getFile() 

where res is the reference to xml configuration files.

I am a newbie to both Maven and Gradle. Could someone please help? Really appreciate!

Upvotes: 1

Views: 262

Answers (1)

julz256
julz256

Reputation: 2712

If what you're trying to achieve is have those xml files available at runtime from within the Jar, then just add your XML files to /src/main/resources.

Anything in that directory automatically gets added to the Jar file and available as classpath resources. Both Maven and Gradle use convention over configuration, where it's a convention to put classpath resources into /src/main/resources as it is to put application Java code in /src/main/java and unit test classpath resources in /src/test/resources and unit test Java code in /src/test/java.

Following Mavan/Gradle conventions will make your configuration simpler too. Unlike Ant where everything needed to be configured.

If your xml files are in common.jar (by putting the xml in common/src/main/resources, and common is on the class path for for app then you should be able to read those files by getting via class loader. E.g SomeClassFromCommon.class.getClassLoader().getResourceAsStream("somefile.xml")

Upvotes: 1

Related Questions