Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate a war file based on two subprojects

Tags:

gradle

war

I have a project which is splitted into two subprojects.

/project
   /sub-project-a (backend with JAVA source which is compiled into JAR file)
   /sub-project-b (frontend sources which are compiled with grunt via gradle call)
   build.gradle
   settings.gradle (contains include 'sub-project-a', 'sub-project-b')

My Question is how can I create a War file with sub-projects and external lib dependencies? The following code snipped is my current build.gradle:

apply plugin: 'war'
version '1.0.0'
repositories {
    mavenCentral()
}
dependencies {
    compile project(':sub-project-a')
    compile project(':sub-project-b')
    compile 'com.google.code.gson:gson:2.2.4'
}
task copy(type: Copy) {
    from 'sub-project-a/build', 'sub-project-b/build'
    into 'build'
}
build.dependsOn clean, copy
war {
    archiveName 'project.war'
}

One detail is important. The java context listener (deep inside project code) work with compiled backend as jar file from WEB-INF/lib folder. This means that all class files can't be easily used from WEB-INF/classes folder.

As you can see I played with dependencies and a custom copy task. I'm not sure what is right gradle way. How should I do this?

SOLUTION

Define with war.from methode, where you get your static sources.

gradle docu from(sourcePaths) - Specifies source files or directories for a copy. The given paths are evaluated as per Project.files().

My changed build.gradle

apply plugin: 'war'
version '1.0.0'
repositories {
    mavenCentral()
}
dependencies {        
    compile 'com.google.code.gson:gson:2.2.4'
}    
war {
    archiveName 'project.war'
    from 'sub-project-a/build/dist', 'sub-project-b/build/dist'
}
like image 269
Ronny Springer Avatar asked Dec 18 '14 15:12

Ronny Springer


1 Answers

SOLUTION (for cleanly closing this question) shamefully taken from the question's originator ;-)

Define subproject dependencies with the "war.from" method, where you get your static sources.

gradle documentation excerpt: from(sourcePaths) - Specifies source files or directories for a copy. The given paths are evaluated as per Project.files().

Ronny's changed build.gradle

apply plugin: 'war'
version '1.0.0'
repositories {
    mavenCentral()
}
dependencies {        
    compile 'com.google.code.gson:gson:2.2.4'
}    
war {
    archiveName 'project.war'
    from 'sub-project-a/build/dist', 'sub-project-b/build/dist'
}
like image 154
Pierre Avatar answered Oct 25 '22 05:10

Pierre