# Build Multi Java Projects

**URL:** <https://discuss.gradle.org/t/build-multi-java-projects/10238>\
**Category:** Help/Discuss\
**Created:** [June 23, 2015, 12:30pm UTC](https://discuss.gradle.org/t/build-multi-java-projects/10238 "2015-06-23T12:30:13Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jadsonjs](https://avatars.discourse-cdn.com/v4/letter/j/54ee81/32.png) [@jadsonjs](https://discuss.gradle.org/u/jadsonjs)\
**Post date:** [June 23, 2015, 12:30pm UTC](https://discuss.gradle.org/t/build-multi-java-projects/10238/1 "2015-06-23T12:30:14Z")

</div>

Hello,

I’m with a problem to compile a java legacy project with a very complicated structure. I can not chage this scructure.  
The system is divided in 2 java project, one with the active code that need to generated a EAR file and other with the unit test classes that need to be compiled and run.

```
  JAVA_PROJECT
           src1
           src2
           src3
           app
                javaproject.ear
                         javaproject1.war
                         javaproject2.war 
                         META-INF
                               application.xml
                         lib
                               *.jar

  JAVA_PROJECT_TEST
            src1_test
            src2_test 
            src3_test

```

I thought to use gradle because many people said that is more flexible that maven. I don´t know if the problem is with me or gradle is not so flexible as I thought. I have been looking in the internet but do not find a way to build my project.

I tried to declare the test project in the file settings.gradle of the JAVA\_PROJECT:

```
project(':JAVA_PROJECT_TEST').projectDir = new File(settingsDir, '../JAVA_PROJECT_TEST')

```

But it never find this project: Project with path ‘:JAVA\_PROJECT\_TEST’ could not be found.

The support a multi projects is not working the multi java projects. When you declare a project in the build file:

```
 project(':JAVA_PROJECT_TEST') {
   
   }

```

The follow error appears: Project with path ‘:JAVA\_PROJECT\_TEST’ could not be found.

What I need is compile the JAVA\_PROJECT, compile the JAVA\_PROJECT\_TEST, execute the unit tests in JAVA\_PROJECT\_TEST, generated the JAVA\_PROJECT.ear with the JAVA\_PROJECT.jar, 2 JAVA\_PROJECT.war and  
a “lib” directory with all \*jar dependences. All this inside the JAVA\_PROJECT.ear file.

1st - build JAVA\_PROJECT generating the JAVA\_PROJECT.jar  
2nd - build JAVA\_PROJECT\_TEST with JAVA\_PROJECT.jar as dependence  
3rd - run JAVA\_PROJECT\_TEST tests  
4th - build JAVA\_PROJECT1.war and build JAVA\_PROJECT2.war  
5th - create e JAVA\_PROJECT.EAR with the JAVA\_PROJECT.jar, JAVA\_PROJECT1.war, JAVA\_PROJECT2.war  
and “lib” directory inside it.

Can someone give a help? Please

---

<div class="post-metadata">

**Author:** ![Francois\_Guillot](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/francois_guillot/32/8566_2.png) [@Francois\_Guillot](https://discuss.gradle.org/u/Francois_Guillot)\
**Post date:** [June 23, 2015, 1:14pm UTC](https://discuss.gradle.org/t/build-multi-java-projects/10238/2 "2015-06-23T13:14:32Z")

</div>

> [@jadsonjs](#):
>
> I tried to declare the test project in the file settings.gradle of the JAVA\_PROJECT:
> 
> project(‘:JAVA\_PROJECT\_TEST’).projectDir = new File(settingsDir, ‘…/JAVA\_PROJECT\_TEST’)

Unfortunately, I think the the root project of a multi-project build must be ‘above’ all subprojects.  
In other terms, the subprojects must be in child directories of the root project.

---

<div class="post-metadata">

**Author:** ![aacirino](https://avatars.discourse-cdn.com/v4/letter/a/3ab097/32.png) [@aacirino](https://discuss.gradle.org/u/aacirino)\
**Post date:** [June 23, 2015, 3:03pm UTC](https://discuss.gradle.org/t/build-multi-java-projects/10238/3 "2015-06-23T15:03:21Z")

</div>

When your project doesn’t follow the directory layout convention defined by Gradle you need to define your layout with source sets. In your case something like this may help:

```
sourceSets {
	main {
		java { srcDirs = ['src1', 'src2', 'src3'] }
	}
	
	test {
		java { srcDirs = ['src1_test', 'src2_test', 'src3_test'] }
		runtimeClasspath = sourceSets.main.output + files(output.resourcesDir) + files(output.classesDir) + configurations.testRuntime
	}
}

```

I don’t work with ear or war, so you will have to look elsewhere on how to setup the source sets for your application. Also take a look at the jar and war tasks.

---

<div class="post-metadata">

**Author:** ![jadsonjs](https://avatars.discourse-cdn.com/v4/letter/j/54ee81/32.png) [@jadsonjs](https://discuss.gradle.org/u/jadsonjs)\
**Post date:** [June 23, 2015, 6:18pm UTC](https://discuss.gradle.org/t/build-multi-java-projects/10238/4 "2015-06-23T18:18:59Z")

</div>

Hello aacirino,

I know that you can define a structure like this:

```
sourceSets {
    main {
        java {
            srcDir 'src1'
            srcDir 'src2'
            srcDir 'src3'
        }
   }
}

```

But this just work to one, and just one, Java Project where the build.gradle file is localized.

I would like to call a build.gradle scritp in another Java Project to run the unit tests and than come back  
to my project and generate the EAR file, with two WAR files inside.

To run the unit test inside de project JAVA\_PROJECT\_TEST I need to add the JAVA\_PROJECT as the dependence of the JAVA\_PROJECT\_TEST. I have a Ant build that make this tasks, but I am migrating to gradle now. Like I said:

1st - build JAVA\_PROJECT generating the JAVA\_PROJECT.jar (OK)  
2nd - build JAVA\_PROJECT\_TEST with JAVA\_PROJECT.jar as dependence  
3rd - run JAVA\_PROJECT\_TEST tests  
4th - build JAVA\_PROJECT1.war and build JAVA\_PROJECT2.war  
5th - create e JAVA\_PROJECT.EAR with the JAVA\_PROJECT.jar, JAVA\_PROJECT1.war, JAVA\_PROJECT2.war  
and “lib” directory inside it.

---

<div class="post-metadata">

**Author:** ![Francois\_Guillot](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/francois_guillot/32/8566_2.png) [@Francois\_Guillot](https://discuss.gradle.org/u/Francois_Guillot)\
**Post date:** [June 23, 2015, 6:43pm UTC](https://discuss.gradle.org/t/build-multi-java-projects/10238/5 "2015-06-23T18:43:18Z")

</div>

One thing you could do is write a script that wraps your gradle calls.  
First you compile the first project (and maybe publish it in a flatDir local repository?)  
Then you call gradle again to compile your second project, and so on.

Again I don’t think this is possible to reference projects as you want, without having a root gradle project on top of all of them

---

<div class="post-metadata">

**Author:** ![aacirino](https://avatars.discourse-cdn.com/v4/letter/a/3ab097/32.png) [@aacirino](https://discuss.gradle.org/u/aacirino)\
**Post date:** [June 23, 2015, 6:57pm UTC](https://discuss.gradle.org/t/build-multi-java-projects/10238/6 "2015-06-23T18:57:41Z")

</div>

Hi Jadson, a regular multi project gradle build has a root project with no sources and the sub-directories contain the sub-projects directory tree. Each sub-project can have its own source set, you can either define the sub-projects settings and configurations in the root project build script or in each of the sub-projects build script.

Then you can set projects dependencies. For instance in project2 build script you would set

```
dependencies {
	compile project(':project1')
}

```

And then call gradle in the root project, as long as you have defined the projects to build in settings.gradle in the root project dir, side by side with the root build script. Then gradle will take care for you of all the dependencies of the tasks, like unit tests and jar packaging.

All the five steps you mentioned you intend to have as outcomes of your build are automatically performed by gradle when you setup you build scripts properly. A multi project layout should be like this, following gradle’s convention:

```
├── rootProject
	|── build.gradle
	|── settings.gradle
	|── project1
	|	|── build.gradle
	|	|── settings.gradle
	|	└── src
	| └── main
	| |	└── java
	| | └── com
	| | └── yourcompany
	| | └── apackage
	| | └── MyClass.java
	| └── test
	| └── java
	| └── com
	| └── yourcompany
	| └── apackage
	| └── MyClassTest.java
	└── project2
		|── build.gradle
		|── settings.gradle
		└── src
			└── main
			|	└── java
			| └── com
			| └── yourcompany
			| └── anotherpackage
			| └── OtherClass.java
			└── test
				└── java
					└── com
						└── yourcompany
							└── anotherpackage
								└── OtherClassTest.java

```

Angelo, do Brasil

---

<div class="post-metadata">

**Author:** ![Francois\_Guillot](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/francois_guillot/32/8566_2.png) [@Francois\_Guillot](https://discuss.gradle.org/u/Francois_Guillot)\
**Post date:** [June 23, 2015, 7:30pm UTC](https://discuss.gradle.org/t/build-multi-java-projects/10238/7 "2015-06-23T19:30:32Z")

</div>

Wow wow wow  
Did you declare the settings.gradle like that?

include(“foo”)  
project(":foo") {  
projectDir = new File("$settingsDir/…/…/bar")  
}

I found this old thread [Reference external project as dependancy?](https://discuss.gradle.org/t/reference-external-project-as-dependancy/6134/3)  
This is close to what you wrote initially, except the missing ‘include’

---

<div class="post-metadata">

**Author:** ![jadsonjs](https://avatars.discourse-cdn.com/v4/letter/j/54ee81/32.png) [@jadsonjs](https://discuss.gradle.org/u/jadsonjs)\
**Post date:** [June 26, 2015, 2:00pm UTC](https://discuss.gradle.org/t/build-multi-java-projects/10238/8 "2015-06-26T14:00:12Z")

</div>

My solution was call an ANT script that make what I want from the Gradle script. Now Gradle will be used just to resolve the project dependences. But generated another problem:

> [@PowerMock with Gradle](https://discuss.gradle.org/t/powermock-with-gradle/10300):
>
> Hello again, This week I am trying to migrate a legacy system to gradle and having a lot of problems. Now, I am trying to run unit test that use powermock calling a ANT script from gradle: configurations { junitAnt } dependencies { compile(group: 'mockito-all', name: 'mockito-all', version: '1.9.5') compile(group: 'org.powermock', name: 'powermock-core', version: '1.6.1') compile(group: 'org.powermock', name: 'powermock-mockito', version: '1.6.1…

All this problem is beacuse I have a very complex legacy build scenario. Totaly out of Gradle or Maven build patterns. How Gradle is more flexible thant Maven, I chose Gradle instead of Maven to make this build.
