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?
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.
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.
Class ProcessResourcesCopies resources from their source to their target directory, potentially processing them. Makes sure no stale resources remain in the target directory.
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.
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.
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.
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.
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:
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.
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With