Jabari Dash Jabari Dash - 11 months ago 129
Groovy Question

Gradle not maintaining directory hierarchy on gradle build

I am starting a project using gradle to build it. The folder structure follows:

java_data_structures
├── build.gradle
├── build
| ├── classes
| | ├── groovy
| | | └── test
| | | └── DoublyLinkedListSpec.class
| | └── java
| | └── main
| | ├── Application.class
| | └── DoublyLinkedList.class
| ├── distributions
| └── other
|
└── src
├── main
│ └── java
│ ├── Application.java
│ ├── graphs
│ ├── maps
│ ├── matricies
│ ├── sets
│ ├── trees
│ ├── vectors
│ | └── DoublyLinkedList.java
│ └── Application.java
└── test
└── groovy
└── vectors
└── DoublyLinkedListSpec.groovy


The issue is that, the folder structure in the
build/classes/java
and
build/classes/groovy
have been changed. It just lumps all of the class files into one folder -
main
and
tests
respectively. This is an issue for me because in my
Application.java
file, I have the following code:

public class Application {

public static void main(String[] args) {
DoublyLinkedList<Integer> list;
Integer[] array = {1, 2, 3};

list = new DoublyLinkedList<Integer>(array);

System.out.println(list);

}
}


I am able to access the
DoublyLinkedList
without using an
import
statement. I would like for my code to look like this:

import vectors.DoublyLinkedList;

public class Application {

public static void main(String[] args) {
DoublyLinkedList<Integer> list;
Integer[] array = {1, 2, 3};

list = new DoublyLinkedList<Integer>(array);

System.out.println(list);

}
}


But, when I run
gradle build
with the above code, I get the following error:

> Task :compileJava
/Users/jod/java_data_structures/src/main/java/Application.java:1: error: package vectors does not exist
import vectors.DoublyLinkedList;
^
1 error


FAILURE: Build failed with an exception.


This is to be expected, as the structure of the
build/classes/java/main
folder has all the class files adjacent to each other without their respective folders. My
gradle.build
file follows:

apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'application'

sourceCompatibility = 1.8
targetCompatibility = 1.8

mainClassName = "Application"

jar {
baseName = 'java_data_structures'
version = '0.0.1'
}

repositories {
mavenCentral()
}

dependencies {
testCompile(
'junit:junit:4.12',
'org.codehaus.groovy:groovy-all:2.4.4',
'org.spockframework:spock-core:1.0-groovy-2.4',
'org.springframework.boot:spring-boot:1.2.1.RELEASE',
'cglib:cglib-nodep:2.2.2'
)
}

javadoc {
destinationDir = new File("${projectDir}/doc/javadoc")
source = sourceSets.main.allJava
classpath = configurations.compile
}


What do I need to do such that my folder hierarchy maintains the same? This is the structure I would like:

java_data_structures
├── build.gradle
├── build
| ├── classes
| | ├── groovy
| | | └── test
| | | └── vectors
| | | └── DoublyLinkedList.class
| | └── java
| | └── main
| | ├── Application.class
| | └── vectors
| | └── DoublyLinkedList.class
| ├── distributions
| └── other
|
└── src
├── main
│ └── java
│ ├── Application.java
│ ├── graphs
│ ├── maps
│ ├── matricies
│ ├── sets
│ ├── trees
│ ├── vectors
│ | └── DoublyLinkedList.java
│ └── Application.java
└── test
└── groovy
└── vectors
└── DoublyLinkedListSpec.groovy

Answer Source

So as indicated in the comments on the question, the issue was a missing package vectors; in the DoublyLinkedList.java file.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download