Home > Software engineering >  Automatically Add built source files in STS 4?
Automatically Add built source files in STS 4?

Time:10-21

Currently I'm stuck with automatically added built source files after compiled.

QueryDSL IS WORKING (automatically added after refresh.). However, MapStruct is not working.

QueryDSL Output is src/main/generated/querydsl, each of projects. MapStruct is default (build/generated/sources/annotationProcessor/java/main) on their projects.

After add source files manually, this code is works, But I want to add source automatically like querydsl.

here is my part of build.gradle.

// Root build.gradle

buildscript {
    ext {
        springBootVersion = '2.7.2'
        dependencyManagementVersion = '1.0.12.RELEASE'
        mysqlVersion = '8.0.30'
        lombokVersion = '6.5.0.3'
        queryDslVersion = '4.4.0'
        queryDslPluginVersion = '1.0.10'
        mapStructVersion = '1.5.3.Final'
        lombokMapstructBindingVersion = "0.2.0"
    }
    repositories {
        mavenCentral()
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }
    dependencies {
        classpath "org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}"
        classpath "io.spring.gradle:dependency-management-plugin:${dependencyManagementVersion}"
        classpath "io.freefair.gradle:lombok-plugin:${lombokVersion}"
        // for QueryDSL
        classpath "gradle.plugin.com.ewerk.gradle.plugins:querydsl-plugin:${queryDslPluginVersion}"
    }
}

subprojects {
    group = 'test.test'
    version '0.0.1-SNAPSHOT'

    apply plugin: 'java'
    apply plugin: 'org.springframework.boot'
    apply plugin: 'io.spring.dependency-management'
    apply plugin: 'io.freefair.lombok'
    apply plugin: 'com.ewerk.gradle.plugins.querydsl'
    
    sourceCompatibility = '11'

    repositories {
        mavenCentral()
    }

    dependencies {
      compile group: 'com.querydsl', name: 'querydsl-jpa'
      annotationProcessor group: 'com.querydsl', name: 'querydsl-apt'

        // for QueryDSL
        // implementation "com.querydsl:querydsl-core:${queryDslVersion}"
        // implementation "com.querydsl:querydsl-jpa:${queryDslVersion}"
        // annotationProcessor "com.querydsl:querydsl-apt:${queryDslVersion}:jpa"
        annotationProcessor "jakarta.persistence:jakarta.persistence-api"
        annotationProcessor "jakarta.annotation:jakarta.annotation-api"
        
        annotationProcessor "org.mapstruct:mapstruct-processor:${mapStructVersion}"
        implementation "org.mapstruct:mapstruct:${mapStructVersion}"
        
        annotationProcessor "org.projectlombok:lombok-mapstruct-binding:${lombokMapstructBindingVersion}"
        
        testImplementation 'junit:junit:4.12'
    }
}

// for QueryDSL
task cleanGeneatedDir(type: Delete) {
    delete file('src/main/generated')
}

// A Project build.gradle

dependencies {
    implementation group: 'org.springframework.boot', name: 'spring-boot-starter-web'
    implementation group: 'org.springframework.boot', name: 'spring-boot-starter-security'
    implementation group: 'org.springframework.boot', name: 'spring-boot-starter-validation'
    implementation group: 'org.springframework.boot', name: 'spring-boot-starter-data-jpa'
    implementation group: 'org.springdoc', name: 'springdoc-openapi-ui', version: '1.6.11'
    implementation 'com.querydsl:querydsl-jpa:4.4.0'
    
    testImplementation group: 'org.springframework.boot', name: 'spring-boot-starter-test'
}

def querydslDir = "src/main/generated/querydsl"

querydsl {
    library = "com.querydsl:querydsl-apt"
    jpa = true
    querydslSourcesDir = querydslDir
}
sourceSets {
    main.java.srcDir querydslDir
}
compileQuerydsl{
    options.annotationProcessorPath = configurations.querydsl
}

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
    querydsl.extendsFrom compileClasspath
}

CodePudding user response:

It automatically configured by sourceSets > main > java > srcDirs in gradle.

in codes,

sourceSets {
    main {
        java {
            srcDirs = ['src/main/java', 'build/generated/sources/annotationProcessor/java/main']
        }
    }
}
  • Related