How to add other projects in current project in Gradle? - interface-builder

I have two projects ProjA and ProjB, here ProjB is depends on ProjA. So, how to include ProjA in ProjB?
And please let me know in case change in the ProjA build.gradle file?

Okay, so you have two projects, whereas ProjA depends on ProjB.
If you have already set up a gradle build for those, both of them will have a file settings.gradle, which at least defines the project name and build.gradle, where you can specify the dependencies.
Example:
settings.gradle
rootProject.name = 'ProjA'
build.gradle
group = 'mygroup'
version = '1.0'
By further adding one of Gradle's publishing plugins (see Maven publishing or Ivy publishing), you can configure a publication. A publication in turn contains artifacts, that is the files you want to upload (and usually some repositories).
Example for a minimal Maven publication:
apply plugin: 'maven-publish'
publishing {
publications {
core(MavenPublication) {
from components.java
}
}
}
The maven-publish plugin will add the task publishToMavenLocal (among others) to your project which installs the jar somewhere to ~/.gradle/caches/.
In build.gradle of ProjB you can then define a compile time dependency on ProjA like so:
dependencies {
compile(group: 'mygroup', name: 'ProjA', version: '1.0')
}

If your projects are co-located, you could have a multi build project.
Say you have a root folder with these contents:
some root directory
project-a
project-a.gradle
src
project-b
project-b.gradle
src
build.gradle
settings.gradle
settings.gradle contains:
include 'project-a', 'project-b'
project-b.gradle then depends on project A:
dependencies {
compile project(':project-a')
}
After this, project a will always compile and assemble before project-b and the jar file for project-a jar will be on the classpath when compiling project-b.

Related

IntelliJ gradle integration issue with independent gradle subprojects

I have two independent gradle projects that I also want to build from one main gradle project:
main-project
build.gradle
settings.gradle
--subproject-one
build.gradle
settings.gradle
--module-a
build.gradle
--subproject-two
build.gradle
settings.gradle
--module-b
build.gradle
My top settings.gradle includes all modules from the sub projects:
file('subproject-one').eachDir { dir ->
if(dir.list().contains("build.gradle")) {
include dir.name
project(":${dir.name}").projectDir = dir
}
}
file('subproject-two').eachDir { dir ->
if(dir.list().contains("build.gradle")) {
include dir.name
project(":${dir.name}").projectDir = dir
}
}
Now i have a dependency from module-b in subproject-two to module-a in subproject-one. I use a compile switch (useProjectDependencies) to include them as project dependencies when I build from the main project and as normal dependencies when i build from the subprojects:
if(useProjectDependencies){
compile project(':module-b')
} else {
//Client dependencies
compile "my.group.id:module-b
}
Via gradle I'm now able to build the main-project including the subproject and also each subproject on it's own.
When I open the main-project now via IntelliJ I see all the modules are recognized correctly and also the cross references to classes between the modules work correctly (refactoring interface between to components are working great).
When I now try to use the gradle integration from Intellij and click on assemble for the module-b then it incorrectly uses the build.gradle form and settings.gradle from subproject-two and doesn't use the project dependencies. It looks like IntelliJ or the gradle daemon searches for the first settings.gradle in a bottom up way from the module-b instead of using the one in the main-project.
When I build via terminal from the main-project folder all works correctly:
gradlew :module-b:assemble //correct *settings.gradle* and *build.gradle* is used
When I delete the settings.gradle and build.gradle from subproject-two then the build.gradle from the main-project is correctly used.
Is there a way to specify the settings.gradle and build.gradle that is used as part of the IntelliJ gradle integration?

How to declare common dependencies in multimodule gradle project on parent folder

I've multi-module gradle project. Let's say project parent has modules module1 and module2.
I have included both the modules in settings.gradle in the parent project as well.
When I declare the common dependencies in build.gradle of parent project, both the project is not compiling but when I add the dependencies in build.gradle of each module then the modules are compiling successfully.
Any Idea how can I do this.
You can declare dependencies for all modules in your project using the allprojects statement in your parent module. This is the essential way of doing so in your parent build.gradle file:
allprojects {
// Plugins can go here, example:
apply plugin: 'java'
dependencies {
// Dependencies go here, example:
compile group: 'org.apache.shiro', name: 'shiro-core', version: '1.4.0'
}
}
Check this and this answer for more insight on the configuration point of a multi project.
Also have a look at the Gradle documentation about this topic for a deep dive.

Collect build artifacts from subprojects into RPM with Gradle

I have the following project structure:
Root
+---Sub1
|
+---Sub2
Sub1 project produces JAR, Sub2 creates WAR.
Root project goal is to produce RPM that contains both the Sub1 and Sub2 output.
I'm trying to use ospackage Gradle plugin to build RPM. Here is part of root build.gradle:
ospackage {
from subprojects.collect { it.tasks.withType(Zip) } into 'lib'
}
It works but for Sub2 both JAR and WAR are created, which is undesirable.
What configuration should be applied in order to collect appropriate jars and wars into RPM?
UPDATE
I'm searching for some generic solution, i.e. iteration over subprojects is preferable to specifying behaviour for each subproject.
The solution is to add WAR plugin in Sub2 project's build.gradle:
apply plugin: 'war'
make buildRpm task dependent on all subprojects build tasks in the root build file:
buildRpm.dependsOn getTasksByName('build', true)
and finally configure ospackage plugin to collect the artifacts into the target RPM:
ospackage {
from(subprojects.collect { "${it.buildDir}/libs" }) {
into 'bin'
}
}

How to generate a war file based on two subprojects

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'
}
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'
}

Using gradle for a flat, multi-module project where settings.gradle is not the highest-level dependency

I have a multi-module project with a directory structure like:
proj
|-modA
|-modB
|-modMain
\-modSysTest
The dependencies are:
modB -> modA
modMain -> modB
modMain -> modA
modSysTest -> modMain
We currently use Ant as our build system. Every module has a build.xml. There is a master.xml that runs the multi-module build, which is in modMain.
I am investigating switching our project to use gradle. I can get a multi-module build working if I put a build.gradle and settings.gradle in a new sibling directory and set up the dependent modules using includeFlat.
I tried moving the gradle files into modMain, which is the home of both the main component of our system as well as the top-level build script (master.xml) that builds the whole system, but I cannot get the build working this way. My main stumbling block at the moment is trying to configure modSysTest to depend on modMain from within modMain.
Here is the settings.gradle from modMain:
includeFlat 'modA', 'modB', 'modSysTest'
Here is the build.gradle file from modMain:
allprojects {
apply plugin: 'java'
repositories {
mavenCentral()
};
dependencies {
testCompile 'junit:junit:4.11'
}
}
project(':modB') {
dependencies {
compile project(':modA')
}
}
project(':modSysTest') {
dependencies {
compile project(':modMain')
}
}
dependencies {
compile project(':modA'), project(':modB')
}
When using this configuration, I get the error:
A problem occurred evaluating root project 'modMain'.
> Project with path ':modMain' could not be found in project ':modSysTest'.
I am not sure how to specify modMain as a dependency for modSysTest.
The project path of the root project is :, not :modMain. It's not very customary (but possible) to have code in the root project. I don't see how putting build.gradle and settings.gradle into a sibling directory of modMain would give a different result (unless you also changed the contents of settings.gradle). Note that with such a setup, Gradle won't find settings.gradle unless you start the build from the modMain directory or pass the location of the settings file with -c.

Resources