我是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构建时,它会在WEB-INF/lib目录下添加jar文件。但是,除了这些jar文件之外,我还需要来自com.system组的jar文件和从bundleJar任务生成的jar文件。我如何才能做到这一点?
发布于 2018-02-13 22:26:52
感谢您对此的回复。真的很感谢。
我已经想出了一种方法。了解到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*'
}*/
}发布于 2018-01-23 19:30:57
默认情况下,classpath中包含来自compile配置的库,但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
}https://stackoverflow.com/questions/48395772
复制相似问题