Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add another java source directory to gradle script

Tags:

java

gradle

I hava an example java project package

package com.example.testing;

with such file tree

app |   src->com->example->testing->Main.java 

and a gradle script:

apply plugin: 'java' apply plugin: 'application'  sourceSets {     main {         java {             srcDirs 'src'         }     }   }  sourceSets.main.output.classesDir = file("classes") mainClassName = 'com.example.testing.Main'  defaultTasks 'compileJava', 'run' 

Now I want to add some module to this project and my folders will be something like this

app |   src1->com->example->testing->Main.java    src2->com->another_example->another_testing->Library.java 

How do I add new source code to gradle script?

like image 815
RedCollarPanda Avatar asked Jun 26 '15 16:06

RedCollarPanda


People also ask

What is Sourcesets in Gradle?

1.1 What is a Gradle SourceSet ? A SourceSet is a collection of java source files and additional resource files that are compiled and assembled together to be executed. The main idea of sourcesets is to group files with a common meaning for the project, with no need of separate them in another project.

What is compileJava in Gradle?

compileJava — JavaCompile. Depends on: All tasks which contribute to the compilation classpath, including jar tasks from projects that are on the classpath via project dependencies. Compiles production Java source files using the JDK compiler.

What is ProcessResources in Gradle?

Class ProcessResourcesCopies resources from their source to their target directory, potentially processing them. Makes sure no stale resources remain in the target directory.

What is buildSrc in Gradle?

buildSrc is a separate build whose purpose is to build any tasks, plugins, or other classes which are intended to be used in build scripts of the main build, but don't have to be shared across builds.

How do I add a Java plugin to a Gradle?

Use the following line in build.gradle file. Whenever you add a plugin to your build, it assumes a certain setup of your Java project (similar to Maven). Take a look at the following directory structure. src/main/java contains the Java source code. src/test/java contains the Java tests.

How do I use sourcesets in Gradle?

SourceSets can be used to specify a different project structure. For example, the sources are stored in a src folder rather than in src/main/java. Take a look at the following directory structure. Gradle does not yet support multiple project templates. But it offers an init task to create the structure of a new Gradle project.

How do I start a Gradle project from a folder?

Create a project folder Gradle comes with a built-in task, called init, that initializes a new Gradle project in an empty folder. The init task uses the (also built-in) wrapper task to create a Gradle wrapper script, gradlew. The first step is to create a folder for the new project and change directory into it.

How do I use a flat directory in Gradle?

Flat Directory If we want to use a flat filesystem directory as our repository, we need to add the following to our build.gradle file: This makes Gradle look into lib1 and lib2 for dependencies. Once we set the flat directories, we can use our local JAR file from the lib1 or lib2 folder:


2 Answers

I agree with @JB Nizet about respecting standard conventions. If you still insist on being an Anarchist though:

You already have src declared in your sourceset, why not add src1 and src2 as well? You can add them to the same sourceset, or define a sourceset per module if you want.

sourceSets {     main {         java {             srcDirs 'src'             srcDirs 'src1'             srcDirs 'src2'         }     }  } 

To reference files outside the project, see this answer.

like image 148
RaGe Avatar answered Oct 06 '22 19:10

RaGe


I have a slightly different approach with a Gradle 4.6:

sourceSets {     main {         java {             srcDir 'src/main/java'             srcDir 'build/swagger-code-dummy/src/main/java'         }     } } 

as you can see, I had to specify the directories with the "/main/java" subdirectories as well, otherwise gradle/intellij was not setting the right path.

Maybe this helps someone else too :)

like image 21
gabowsky Avatar answered Oct 06 '22 20:10

gabowsky