[Gradle] Build a Java project with a configuration different from the convention

Overview

Gradle adopts the idea of convention, so it is not the default folder structure Unable to build project. However, in Gradle, by changing the configuration and properties It is possible to flexibly change those rules.

Property approach

As an example, change the Java source and resource placement conventions for the main source set.

This time, build a project with the following configuration.

Project structure


<project-root>
└ src
    ├ Mapping.xml
    ├ app.properties
    └ examples
        └ Dummy.java

In Gradle, Java source is under src / main / java directory, Resources are expected to be located under the src / main / resources directory.

However, in the above project, Java sources and resources are put together in the src directory. Therefore, by setting the Java source and resource placement path in the build script Build a project that doesn't follow the default conventions.

apply plugin: 'java'

sourceSets {
    main {
        java {
            // sourceSets.main.java.Add settings to srcDirs property
            // =Set the Java source location
            srcDir 'src'
        }
        resources {
            // sourceSets.test.java.Add settings to srcDirs property
            // =Set the resource placement destination
            srcDir 'src'
        }
    }
}

processResources {
    // .Exclude directories that do not have anything other than java files from copying
    includeEmptyDirs = false
}

Approach from configuration

What is configuration?

The intent is a unit for grouping and classifying dependencies. Actually, only the path referenced by the task (= task processing target) is set.

The Java plug-in configurations are as follows.

Use of configuration

For example, if you want to set a library dependency, Suppose you want to apply JUnit library additions only to test-related tasks. In that case, to set the library dependencies only for the compileTestJava task Make settings for the testCompile configuration.

dependencies {
    testCompile 'junit:junit:4.11'
}

Approach from the source set

Add source set

You can add the source set by writing the following in the build script.

sourceSets {
    exampleSet {
        java.srcDir file('src/example/java')
    }

Automatically added configuration

When you add the source set, the following configurations are automatically added.

To set the above configuration, write the following in the build script.

sourceSets {
    exampleSet {
        compileClasspath = 'src/ex/java'
        runtimeClasspath = 'build/ex/classes'
    }
}

Tasks added automatically

When you add the source set, the following tasks are added automatically.

Add task

Below, through the addition of a source set (integrationTest) for performing integration tests Try adding a Test type task.

sourceSets {
    integrationTest {
        //Test code for integration test (Java source)Set the placement destination of
        java.srcDir file('src/intTest/java')
        //See the output class of the main source set and the compile-time classpath of the test source set at compile time
        compileClasspath = sourceSets.main.output + configration.testCompile
        //See your own output class at runtime, the compile-time classpath, and the same classpath as the test source set at runtime
        runtimeClasspath = output + compileClasspath + configradion.testCompile
    }
}

task doIntegrationTest(type: Test) {
    //Test the output of the source set integrationTest
    testClassDir = sourceSets.integrationTest.output.classDir

    //Set the integrationTest runtimeClasspath in the classpath when running the test
    classpath = sourceSets.integrationTest.runtimeClasspath

reference

This article is written with reference to the following books.

Reference book: Thorough introduction to Gradle Building an automation platform with next-generation build tools

Recommended Posts

[Gradle] Build a Java project with a configuration different from the convention
Build a Java project with Gradle
[Java] [Play Framework] Until the project is started with Gradle
I want to return a type different from the input element with Java8 StreamAPI reduce ()
CICS-Run Java applications-(3) Build management with Gradle
Build and test Java + Gradle applications with Wercker
Using Gradle with VS Code, build Java → run
Build Spring Boot project by environment with Gradle
Java tips-Create a Spring Boot project in Gradle
Add a project in any folder with Gradle
Automate Java (Maven) project build with CircleCI + Orbs
Build a Java development environment with VS Code
[Note] Java: Create a simple project while learning how the configuration file works.
Build VS Code + WSL + Java + Gradle environment from scratch
Create a Java (Gradle) project with VS Code and develop it on a Docker container
Call a method with a Kotlin callback block from Java
java build a triangle
First gradle build (Java)
Using the database (SQL Server 2014) from a Java program 2018/01/04
[Note] Create a java environment from scratch with docker
[Environment construction] Build a Java development environment with VS Code!
Check coverage with Codecov in Java + Gradle + Wercker configuration
Build Java with Wercker
Until you build a project described in scala with Maven and execute it with the scala command.
Find the address class and address type from the IP address with Java
Create a simple DRUD application with Java + SpringBoot + Gradle + thymeleaf (1)
Java multi-project creation with Gradle
Using Docker from Java Gradle
One file of Docker x Laravel threat! Build a local development environment with the minimum configuration
With [AWS] CodeStar, you can build a Spring (Java) project running on Lambda in just 3 minutes! !!
AWS Elastic Beanstalk # 1 with Java starting from scratch-Building a Java web application environment using the EB CLI-
Create a simple web server with the Java standard library com.sun.net.httpserver
Try Hello World with the minimum configuration of Heroku Java spring-boot
Access the in-memory data grid Apache Ignite from a Java client
Replace with a value according to the match with a Java regular expression
A story about hitting the League Of Legends API with JAVA
[Java] From new project creation to automatic test / build / deployment realization
The story of making it possible to build a project that was built by Maven with Ant
You cannot change the project facet version from a dynamic web module to x.x. How to deal with
Build a Node.js environment with Docker
Build a Tomcat 8.5 environment with Pleiades 4.8
Code Java from Emacs with Eclim
[Rails] Creating a new project with rails new
Run a batch file from Java
Build a web application with Javalin
Create a Java project using Eclipse
Java build with mac vs code
Follow the link with Selenium (Java)
Work with Google Sheets from Java
The road from JavaScript to Java
Create a Maven project with a command
Spring Boot gradle build with Docker
Find the address class and address type from the IP address with Java [No. 2 decoction]
A collection of phrases that impresses the "different feeling" of Java and JavaScript
Make a daily build of the TOPPERS kernel with Gitlab and Docker
The story of making a game launcher with automatic loading function [Java]
Let's express the result of analyzing Java bytecode with a class diagram
[Java] Create a jar file with both compressed and uncompressed with the jar command
I can't build if I set the build destination to a simulator with XCode12!
Try to build a reverse proxy type configuration with Keycloak (Security Proxy edition)
<java> Split the address before and after the street address with a regular expression