我是Gradle的新手。使用Gradle 3.5,我试图从java项目创建war构建。以下是我的gradle文件内容。
apply plugin: 'war'
buildDir = "${rootProject.ext.buildGradle}/${project.name}"
def buildClassesDir = buildDir.getAbsolutePath() + '/classes/main'
configurations {
localLibraries
}
task copyNonJava(type: Copy, dependsOn: compileJava) {
from ('src/main/java') {
exclude '**/*.java'
include '**/*.properties'
}
from ('resources') {
include 'default_content.properties'
}
into buildClassesDir
includeEmptyDirs = false
}
task bundleJar (type: Jar, dependsOn: ['compileJava', 'copyNonJava']) {
baseName archivesBaseName
from buildClassesDir
}
task bundleWar (type: War, dependsOn: ['bundleJar']) {
baseName = project.name
from 'web'
webXml = file( 'resources/WEB-INF/web.xml' )
classpath = configurations.localLibraries
}
dependencies {
compile group: 'com.system', name: 'core', version: rootProject.version, changing: true
compile group: 'com.system', name: 'core-ui', version: rootProject.version, changing: true
compile group: 'com.persistence', name: 'persistence', version: '1.0'
compile group: 'com.surveys', name: 'survey', version: '1.0'
localLibraries fileTree("lib") {
exclude 'spring*'
}
}
当我生成war build时,它会在WEB-INF/lib
目录下添加jar文件。但是,除了那些jar文件,我还需要来自com.system
组的jar文件和从bundleJar
任务生成的jar文件。我怎样才能做到这一点?
答案 0 :(得分:0)
compile
配置中的库默认包含在classpath
中,但classpath = configurations.localLibraries
会覆盖默认值。
而不是overriding the default classpath(classpath = ...
实际上意味着setClasspath(...)
)你可以append additional files to it:
task bundleWar (type: War, dependsOn: ['bundleJar']) {
baseName = project.name
from 'web'
webXml = file( 'resources/WEB-INF/web.xml' )
classpath configurations.localLibraries
classpath bundleJar.archivePath
}
答案 1 :(得分:0)
感谢您对此的回应。真的很感激。
我已经找到了解决这个问题的方法。了解gradle遵循一些目录约定,因此更改gradle支持的目录结构并更改gradle脚本。
这是使用gradle脚本:
apply plugin: 'war'
buildDir = "${rootProject.ext.buildGradle}/${project.name}"
def buildClassesDir = buildDir.getAbsolutePath() + '/classes/main'
configurations {
localLibraries
}
task bundleJar (type: Jar, dependsOn: ['compileJava']) {
baseName archivesBaseName
from buildClassesDir
}
task bundleWar (type: War, dependsOn: ['bundleJar']) {
dependsOn = [ 'bundleJar' ]
baseName = project.name
from 'web'
webXml = file( 'resources/WEB-INF/web.xml' )
}
dependencies {
compile group: 'com.system', name: 'core', version: rootProject.version, changing: true
compile group: 'com.system', name: 'core-ui', version: rootProject.version, changing: true
compile group: 'com.persistence', name: 'persistence', version: '1.0'
compile group: 'com.surveys', name: 'survey', version: '1.0'
// Replace this and make sure all necessary jar dependencies are been fetched from repository instead from file system
/*localLibraries fileTree("lib") {
exclude 'spring*'
}*/
}