2012-02-21 70 views
6

我的Gradle構建腳本中存在重複文件問題。Gradle構建的.war文件中的重複文件

我的目錄結構maven的標準,再加上各種構建配置一些額外的目錄:

/src/main/java 
/src/main/resources 
/src/main/dev/resources 
/src/main/prod/resources 

/src/main/resources/src/main/dev/resources這些文件顯然是由processResourceswar任務既處理,並在結束.war文件兩次。 如何防止發生這種情況,而不需要手動排除戰爭配置中的每個文件?

我的整個build.gradle包含在下面; note buildEnvironment默認設置爲dev,但也可以是prod

apply plugin: "sonar" 
apply plugin: "war" 
apply plugin: "eclipse-wtp" 

// ************************************************************************************************ 
// GENERAL CONFIGURATION 
// ************************************************************************************************ 

sourceCompatibility = 1.6 
group = "com.foo" 
archivesBaseName = "security" 
version = "0.1-SNAPSHOT" 

// versions of various components where we need more than one and may want to update often 
def springVersion = "3.1.1.RELEASE" 
def tomcatVersion = "7.0.25" 
def jasperVersion = "4.5.0" 

// buildEnvironment is set in gradle.properties and can be overridden with -PbuildEnvironment=... on the command line 
println "running in $buildEnvironment mode..." 

// set classes output directory to WEB-INF/classes 
eclipse.classpath.defaultOutputDir = new File(project.getWebAppDir().getAbsolutePath(), "/WEB-INF/classes") 

// ************************************************************************************************ 
// SOURCE SETS 
// ************************************************************************************************ 

sourceSets { 
    // add the resources specific to the build environment 
    main.resources.srcDirs += "src/main/$buildEnvironment/resources" 
    // add source set for jasper reports 
    jasperreports { 
    srcDir = file(relativePath('src/main/jasperreports')) 
    output.classesDir = file(relativePath('src/main/java/com/foo/bar/security/statistics')) 
    } 
} 

// ************************************************************************************************ 
// PLUGINS 
// ************************************************************************************************ 

buildscript { 
    repositories { 
    add(new org.apache.ivy.plugins.resolver.URLResolver()) { 
     name = 'GitHub' 
     addArtifactPattern 'http://cloud.github.com/downloads/[organisation]/[module]/[module]-[revision].[ext]' 
    } 
    } 

    dependencies { classpath 'bmuschko:gradle-tomcat-plugin:0.9' } 
} 

apply plugin: "tomcat" 

// ************************************************************************************************ 
// PLUGIN CONFIGURATION 
// ************************************************************************************************ 

// configure eclipse .project/.classpath generator 
eclipse { 
    project { natures 'com.springsource.sts.gradle.core.nature' } 
    wtp { component { contextPath = "/security" } } 
} 

configurations { 
    // make sure we don't get dependencies we don't want 
    all*.exclude group: "net.sf.ehcache", module: "ehcache-terracotta" 
    all*.exclude group: "bouncycastle", module: "bcmail-jdk14" 
    all*.exclude group: "bouncycastle", module: "bcprov-jdk14" 
    all*.exclude group: "bouncycastle", module: "bctsp-jdk14" 

    // wtp needs a special invitation for some reason 
    eclipseWtpComponent { 
    exclude group: "net.sf.ehcache", module: "ehcache-terracotta" 
    } 

    jasperreports { transitive = true } 
} 

// maven repositories 
repositories { 
    maven { url "http://maven.springframework.org/milestone/" } 
    mavenCentral() 
} 

// sonar configuration 
sonar { 
    server { url = "http://xxx" } 
    database { 
    url = "jdbc:mysql://xxx" 
    driverClassName = "com.mysql.jdbc.Driver" 
    username = "xxx" 
    password = "xxx" 
    } 
    project { key = "foo.bar:security" } 
} 

war { 
    // set war output file name 
    archiveName = "security.war" 
    // make sure no duplicate processing of files takes place 
    excludes += [ 
    "**/database.properties", 
    "**/logback.xml", 
    "**/rebel.xml", 
    "**/upload.properties", 
    "**/ValidationMessages.properties" 
    ] 
} 

tomcatRun { contextPath = "/security" } 

// ************************************************************************************************ 
// DEPENDENCIES 
// ************************************************************************************************ 

dependencies { 

    // exclusions for jasperreports, which tries to load old versions of stuff 
    compile("net.sf.jasperreports:jasperreports:$jasperVersion") { 
    exclude module: "jfreechart" 
    exclude module: "jcommon" 
    } 

    // exclusions for ehcache, we don't want their enterprise cache 
    compile("net.sf.ehcache:ehcache:2.5.1") { 
    exclude group: "net.sf.ehcache", module: "ehcache-terracotta" 
    } 

    // compile and runtime dependencies 
    compile "org.springframework:spring-webmvc:$springVersion", 
     "org.springframework:spring-orm:$springVersion", 
     "org.springframework:spring-aspects:$springVersion", 
     "org.springframework.mobile:spring-mobile-device:1.0.0.RC1", 
     "org.jfree:jfreechart:1.0.14", 
     "org.apache.tiles:tiles-jsp:2.2.2", 
     "c3p0:c3p0-oracle-thin-extras:0.9.1.2", 
     "org.mybatis:mybatis-spring:1.0.2", 
     "org.aspectj:aspectjrt:1.6.12", 
     "org.aspectj:aspectjweaver:1.6.12", 
     "org.codehaus.jackson:jackson-mapper-asl:1.9.4", 
     "ch.qos.logback:logback-classic:1.0.0", 
     "org.slf4j:jcl-over-slf4j:1.6.4", 
     "org.slf4j:log4j-over-slf4j:1.6.4", 
     "org.slf4j:jul-to-slf4j:1.6.4", 
     "org.hibernate:hibernate-validator:4.2.0.Final", 
     "com.google.guava:guava:11.0.1", 
     "commons-dbutils:commons-dbutils:1.4", 
     "commons-fileupload:commons-fileupload:1.2.2", 
     "commons-io:commons-io:2.1", 
     "commons-lang:commons-lang:2.6", 
     "org.bouncycastle:bcprov-jdk16:1.46", 
     "org.quartz-scheduler:quartz:2.1.3", 
     "jdom:jdom:1.1", 
     "cglib:cglib:2.2.2", 
     "org.jasypt:jasypt:1.9.0", 
     "com.sun.mail:smtp:1.4.4", 
     "com.sun.mail:mailapi:1.4.4", 
     "xalan:xalan:2.7.1", 
     "org.jdom:saxpath:1.0-FCS" 

    runtime "javax.servlet:jstl:1.2" 

    // for compiling jasper reports 
    jasperreports "net.sf.jasperreports:jasperreports:$jasperVersion", 
     "org.codehaus.groovy:groovy-all:1.8.6" 

} 

// dependencies for each tomcat version, which are in different packages for 6.x and 7.x, sigh 
println "adding dependencies for Tomcat $tomcatVersion" 
if (tomcatVersion.startsWith("6")) { 
    dependencies.add("providedCompile", "org.apache.tomcat:catalina:$tomcatVersion") 
    dependencies.add("tomcat", "org.apache.tomcat:catalina:$tomcatVersion") 
    dependencies.add("tomcat", "org.apache.tomcat:coyote:$tomcatVersion") 
    dependencies.add("tomcat", "org.apache.tomcat:jasper:$tomcatVersion") 
} else if (tomcatVersion.startsWith("7")) { 
    dependencies.add("providedCompile", "org.apache.tomcat:tomcat-catalina:$tomcatVersion") 
    dependencies.add("tomcat", "org.apache.tomcat:tomcat-catalina:$tomcatVersion") 
    dependencies.add("tomcat", "org.apache.tomcat:tomcat-coyote:$tomcatVersion") 
    dependencies.add("tomcat", "org.apache.tomcat:tomcat-jasper:$tomcatVersion") 
} 

// ************************************************************************************************ 
// JASPER REPORTS 
// ************************************************************************************************ 

task jasperReports(overwrite: true) << { 
    ant { 
    taskdef(name: 'jrc', 
     classname: 'net.sf.jasperreports.ant.JRAntCompileTask', 
     classpath: configurations.jasperreports.asPath) 
    mkdir(dir:sourceSets.jasperreports.output.classesDir) 
    jrc(srcdir: sourceSets.jasperreports.srcDir, destdir: sourceSets.jasperreports.output.classesDir) { 
     include(name:'**/*.jrxml') 
     classpath { 
     pathElement(path: configurations.jasperreports.asPath) 
     } 
    } 
    } 
} 

task cleanJasperReports(overwrite: true) << { 
    ant.delete() { 
    fileset(dir:sourceSets.jasperreports.output.classesDir, includes: "*.jasper") 
    } 
} 

compileJava.dependsOn jasperReports 

回答

3

我沒有測試你的配置在這裏,但我認爲那是因爲你要添加main.resources.srcDirs += "src/main/$buildEnvironment/resources"到sourceSets。通過添加該你現在以下sourceSets如果buildEnvironment = 'dev'

  • 的src/main/java的
  • 的src/main /資源
  • 的src/main /開發/資源

這意味着,如果您在src/main/resourcessrc/main/dev/resources中都有一個名爲myfile.txt的資源,您將獲得以下文件:

  • WEB-INF /班/ myfile.txt的
  • WEB-INF/classes中的/ dev/myfile.txt的

爲了解決這個問題,你可以只移動你的開發/生產資源src/main之外,恩。像這樣:

  • 的src/main/java的
  • 的src/main /資源
  • SRC的/ dev /資源
  • 的src /生產/資源

,並使用以下sourceSet代替:

main.resources.srcDirs += "src/$buildEnvironment/resources" 

然後所有的dev/prod資源將覆蓋res源於src/main/resources

+0

這就是我所做的,它工作得很好。對不起,我現在只看到了這個,電子郵件必須被埋在垃圾郵件文件夾中...... – gschmidl 2012-10-15 13:44:11

相關問題