![](/img/trans.png)
[英]Gradle - Is there a possibility to compile a single Java class before the rest of the project?
[英]Task in gradle to include java class before compile
我需要在 Gradle 構建之前創建可以替換的 Gradle 任務。java 文件。 Gradle 構建 package 應用程序 in.war 文件。 而且我需要在構建后在那里替換字節碼。 我嘗試了 sourceSets Gradle 任務,但它只能排除文件。
sourceSets {
main {
java {
exclude 'com/myapp/example/resource/impl/ResourceBundleImpl.java'
}
}
}
但我還需要在同一個地方包含文件。 我怎么能用 Gradle 做到這一點?
The directory to file that I need to exclude: com/myapp/example/resource/impl/ResourceBundleImpl.java
The directory to file that I need to include: src/main/webapp/WEB-INF/my/ResourceBundleImpl.java
復制文件內容也是可行的解決方案。但是在編譯之前我該怎么做呢?
以下任務沒有幫助,因為在構建文件中有.java 文件而不是.classe 文件。
task prepareSources(type: Copy) {
from('src/main/webapp/WEB-INF/my')
into('build/classes/java/main/com/myapp/example/resource/impl')com/myapp/example
}
// Prepare sources, before compile
compileJava {
dependsOn prepareSources
}
以下任務拋出: Task:cdx-war:compileJava FAILED error: package com.myapp.example.util 不存在
sourceSets {
main {
java {
srcDir "$projectDir"
exclude 'com/medtronic/diabetes/carelink/rbps/resource/impl/ResourceBundleImpl.java'
include 'src/main/webapp/WEB-INF/my/ResourceBundleImpl.java'
}
}
我將需要替換的類放在一起,並為其中一個添加后綴,並在編譯時替換此后綴:
sourceSets {
main {
java {
srcDirs = ["$buildDir\\generated-src"]
}
}
}
task copySourceSet(type: Copy) {
println 'Change java class in patient-svc-war'
from "$rootDir\\patient-svc-war\\src\\main\\java"
into "$buildDir\\generated-src"
from file("$rootDir\\patient-svc-war\\**_suffix.java")
into "$buildDir\\generated-src"
rename { String fileName ->
fileName.replace("_suffix", "")
}
filter { line -> line.replaceAll('_suffix', '')}
}
compileJava.source "$buildDir\\generated-src"
compileJava {
dependsOn copySourceSet
}
compileJava.doLast {
delete "$buildDir\\generated-src"
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.