简体   繁体   English

使用Eclipse WTP忽略Gradle传递项目依赖项

[英]Ignore gradle transitive project dependencies using eclipse WTP

I have a war project (project A) that has a compile dependency on another project that contains shared libraries (project B), however I don't want the transitive dependencies of the project B to be included on the WTP deployment. 我有一个战争项目(项目A),该项目对包含共享库的另一个项目(项目B)具有编译依赖性,但是我不希望项目B的传递性依赖性包含在WTP部署中。

When I generate the war file using gradle the transitive dependencies from Project B are ignored as I want to, however WTP keeps copying all libs into the /WEB-INF/lib directory and therefore I have classloader issues. 当我使用gradle生成war文件时,我想忽略Project B的传递依赖关系,但是WTP会将所有库都复制到/ WEB-INF / lib目录中,因此存在类加载器问题。

I have tried to to ignore transitive dependencies from Project B using transitive = false and ignoring specific dependencies with exclude both at dependency and configuration level with not success so far and I can't find what I'm doing wrong. 我试图使用transitive = false忽略Project B的可传递依赖关系,并忽略了在依赖关系和配置级别上同时exclude特定依赖关系,但到目前为止还没有成功,我找不到我做错了什么。

Thanks in advance. 提前致谢。

My configuration is as follows: 我的配置如下:

Project A 项目A

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'eclipse-wtp'
apply plugin: 'war'

repositories {
    mavenLocal()
    mavenCentral()
    maven { // ext release
        url "${artifactory_contextUrl}/ext-release-local"
        credentials {
            username = "${artifactory_user}"
            password = "${artifactory_password}"
        }
    }
    maven { // libs release
        url "${artifactory_contextUrl}/libs-release-local"
        credentials {
            username = "${artifactory_user}"
            password = "${artifactory_password}"
        }
    }
    maven { // libs snapshot
        url "${artifactory_contextUrl}/libs-snapshot-local"
        credentials {
            username = "${artifactory_user}"
            password = "${artifactory_password}"
        }
    }
    maven { // SpringSource Enterprise Bundle Repository - SpringSource Bundle Releases
        url "http://repository.springsource.com/maven/bundles/release" }
    maven { // SpringSource Enterprise Bundle Repository - External Bundle Releases
        url "http://repository.springsource.com/maven/bundles/external" }
    maven { // SpringSource Enterprise Bundle Repository - SpringSource Library Releases
        url "http://repository.springsource.com/maven/libraries/release" }
    maven { // SpringSource Enterprise Bundle Repository - External Library Releases
        url "http://repository.springsource.com/maven/libraries/external" }
}

group = 'com.thisproject'

sourceCompatibility = '1.6'

version = '1.6'

war {
    baseName = 'ROOT'
    archiveName = baseName+'.'+extension
    destinationDir = file('build/')
}

task deployToFolder(dependsOn: 'war') << {
    copy {
        from war.archivePath
        into "${deployFolder}"
    }
}

task jenkinsTest{
    inputs.files test.outputs.files
    doLast{
        def timestamp = System.currentTimeMillis()
        test.testResultsDir.eachFile { it.lastModified = timestamp }
    }
}

build.dependsOn(jenkinsTest)

configurations {
    runtime.exclude group: 'commons-validator', module: 'commons-validator'
    runtime {
        transitive = false
    }
}

dependencies{
    providedCompile group: 'log4j', name: 'log4j', version: '1.2.16'
    providedCompile group: 'org.springframework.security', name: 'org.springframework.security.web', version: '3.1.1.RELEASE'
    providedCompile group: 'org.springframework.security', name: 'spring-security-config', version: '3.1.1.RELEASE'
    providedCompile group: 'org.springframework.social', name: 'spring-social-web', version: '1.0.2.RELEASE'
    providedCompile group: 'org.springframework.social', name: 'spring-social-core', version: '1.0.2.RELEASE'
    providedCompile group: 'org.springframework.social', name: 'spring-social-facebook', version: '1.0.1.RELEASE'
    providedCompile group: 'org.springframework.social', name: 'spring-social-twitter', version: '1.0.2.RELEASE'
    providedCompile group: 'spring-social-google', name: 'spring-social-google', version: '1.0'
    providedCompile group: 'jstl', name: 'jstl', version: '1.2'
    providedCompile group: 'org.springframework.security', name:'org.springframework.security.taglibs', version:'3.1.1.RELEASE'

    compile(project(':projectB')) {
        exclude module:'commons-validator'
            transitive = false
    }

    providedCompile group: 'javax.servlet', name: 'servlet-api', version: '2.5'
    providedCompile group: 'javax.servlet', name: 'jsp-api', version: '2.0'
    providedCompile group: 'org.springframework', name: 'org.springframework.transaction', version: '3.1.1.RELEASE'
    providedCompile group: 'commons-validator', name: 'commons-validator', version: '1.2.0'
    providedCompile group: 'commons-lang', name: 'commons-lang', version: '2.4'

    providedCompile group: 'javax.validation', name: 'validation-api', version: '1.0.0.GA'
    providedCompile 'javax.persistence:persistence-api:1.0.2'
    providedCompile group: 'org.hibernate', name: 'hibernate-core', version: '3.6.10.Final'
}

eclipse {
    project { natures 'org.springsource.ide.eclipse.gradle.core.nature' }
    wtp {
        component { deployName = 'ROOT' }
        facet {
            facet name: 'jst.web', version: '2.5'
            facet name: 'jst.java', version: '1.6'
        }
    }
}

compileJava { 
  options.encoding = "UTF-8" 
}

Project B 项目B

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'maven'

repositories {
    mavenLocal()
    mavenCentral()
    maven { // ext release
        url "${artifactory_contextUrl}/ext-release-local"
        credentials {
            username = "${artifactory_user}"
            password = "${artifactory_password}"
        }
    }
    maven { // libs release
        url "${artifactory_contextUrl}/libs-release-local"
        credentials {
            username = "${artifactory_user}"
            password = "${artifactory_password}"
        }
    }
    maven { // libs snapshot
        url "${artifactory_contextUrl}/libs-snapshot-local"
        credentials {
            username = "${artifactory_user}"
            password = "${artifactory_password}"
        }
    }
    maven { // SpringSource Enterprise Bundle Repository - SpringSource Bundle Releases
        url "http://repository.springsource.com/maven/bundles/release"
    }
    maven { // SpringSource Enterprise Bundle Repository - External Bundle Releases
        url "http://repository.springsource.com/maven/bundles/external"
    }
    maven { // SpringSource Enterprise Bundle Repository - SpringSource Library Releases
        url "http://repository.springsource.com/maven/libraries/release"
    }
    maven { // SpringSource Enterprise Bundle Repository - External Library Releases
        url "http://repository.springsource.com/maven/libraries/external"
    }
}

group = 'com.thisproject'

sourceCompatibility = '1.6'

version = '1.7.26-SNAPSHOT'

jar {
    manifest {
        attributes 'Implementation-Title': 'Gradle Quickstart', 'Implementation-Version': version
    }
    destinationDir = file('build/')
}

task deployToFolder(dependsOn: 'jar') << {
    copy {
        from jar.archivePath
        into "${deployFolder}"
    }
}

task sourcesJar(type: Jar, dependsOn:classes) {
    classifier = 'sources'
    from sourceSets.main.allSource
}

artifacts {
    archives sourcesJar
}

dependencies{
    compile group: 'log4j', name: 'log4j', version: '1.2.16'
    compile group: 'commons-logging', name: 'commons-logging', version: '1.1.1'
    compile group: 'com.lowagie', name: 'itext', version: '2.0.8'
    compile group: 'commons-codec', name: 'commons-codec', version: '1.6'
    compile group: 'commons-lang', name: 'commons-lang', version: '2.4'
    compile group: 'commons-io', name: 'commons-io', version: '1.4'
    compile group: 'commons-validator', name: 'commons-validator', version: '1.2.0'
    compile group: 'org.apache.axis', name: 'axis-jaxrpc', version: '1.4'
    compile group: 'com.esendex', name: 'esendex.sdk', version: '1.0'
    compile group: 'org.apache.lucene', name: 'lucene-core', version: '1.9.1'
    compile group: 'org.apache.lucene', name: 'lucene-snowball', version: '1.9.1'
    compile group: 'com.sun.jersey', name: 'jersey-client', version: '1.12'
    compile group: 'com.sun.jersey', name: 'jersey-json', version: '1.12'
    compile group: 'com.sun.jersey.ri', name: 'jax-rs-ri', version: '1.12'
    compile group: 'org.codehaus.jackson', name: 'jackson-mapper-asl', version: '1.9.2'
    compile group: 'org.hibernate', name: 'hibernate-core', version: '3.6.10.Final'
    compile group: 'org.hibernate', name: 'hibernate-validator', version: '4.3.1.Final'
    compile group: 'org.springframework', name: 'org.springframework.aop', version: '3.1.1.RELEASE'
    compile group: 'org.springframework', name: 'org.springframework.beans', version: '3.1.1.RELEASE'
    compile group: 'org.springframework', name: 'org.springframework.context', version: '3.1.1.RELEASE'
    compile group: 'org.springframework', name: 'org.springframework.jdbc', version: '3.1.1.RELEASE'
    compile group: 'org.springframework', name: 'org.springframework.orm', version: '3.1.1.RELEASE'
    compile group: 'org.springframework', name: 'org.springframework.oxm', version: '3.1.1.RELEASE'
    compile group: 'org.springframework', name: 'org.springframework.transaction', version: '3.1.1.RELEASE'
    compile group: 'org.springframework', name: 'org.springframework.web.servlet', version: '3.1.1.RELEASE'
    compile group: 'spring-social-jpa', name: 'spring-social-jpa', version: '0.0.1'
    compile group: 'javax.xml.bind', name: 'jsr173_api', version: '1.0'
    compile group: 'javax.validation', name: 'validation-api', version: '1.0.0.GA'
    compile group: 'com.googlecode.libphonenumber', name: 'libphonenumber', version: '4.1'
    compile group: 'axis', name: 'axis', version: '1.4'
    compile group: 'org.apache.commons', name: 'commons-email', version: '1.2'
    compile group: 'org.apache.tomcat', name: 'catalina', version: '6.0.35'
    compile group: 'javax.servlet', name: 'servlet-api', version: '2.5'
    compile group: 'javax.servlet', name: 'jsp-api', version: '2.0'
    compile group: 'org.springframework.security', name: 'org.springframework.security.web', version: '3.1.1.RELEASE'
    compile group: 'commons-httpclient', name: 'commons-httpclient', version: '3.0.1'
    compile group: 'net.sf.opencsv', name: 'opencsv', version: '2.0'

    testCompile 'junit:junit:4.11'
    testCompile group: 'org.springframework', name: 'org.springframework.test', version: '3.1.1.RELEASE'
    testCompile 'mysql:mysql-connector-java:5.1.22'
    testCompile group: 'org.hibernate', name: 'hibernate-core', version: '3.6.10.Final'
    testCompile group: 'org.hibernate', name: 'hibernate-entitymanager', version: '3.6.10.Final'
    testCompile 'org.hibernate:hibernate-validator:4.3.1.Final'

    compile(group: 'com.paypal.sdk', name: 'merchantsdk', version: '2.2.98')
    compile(group: 'com.paypal.sdk', name: 'paypal-core', version: '1.1.1')
}

compileJava { 
  options.encoding = "UTF-8" 
}

After a massive search through net and digging into so many Gradle and WTP stuff, I was able to get this working. 在通过网络进行大量搜索并挖掘了这么多Gradle和WTP内容之后,我得以使它工作。 Basically, there are three ways to ignore dependencies in WTP deploy directory (or I am aware of only three of them!). 基本上,有三种方法可以忽略WTP部署目录中的依赖性(或者我只知道其中三种!)。

1. Setting transitive to false 1.将传递条件设置为false
As you have mentioned in the question and the syntax is like: 正如您在问题中提到的,语法类似于:

compile('group.id:jar.name:1.0') {transitive = false}
runtime('group.id:jar.name:1.0') {transitive = false}

This should prevent the dependant JARs of being added to final WTP directory. 这应防止将依赖的JAR添加到最终WTP目录中。

2. Excluding using provided syntax 2.不包括使用提供的语法
Gadle comes with a feature to prevent dependencies of being added to final WAR file which in here can be used to prevent them of being copied to WTP deploy dir. Gadle带有一项功能,可以防止将依赖项添加到最终的WAR文件中,在此处可以使用该功能来防止将依赖项复制到WTP部署目录中。 And here is the syntax: 这是语法:

providedCompile 'group.id:jar.name:1.0'
providedRuntime 'group.id:jar.name:1.0'

3. HACK!! 3.哈克!
Here comes the most interesting part. 这是最有趣的部分。 What if none of the above works? 如果以上都不起作用怎么办? I found this thread on STS jira which is about same issue and that pointed me to this solution which did not work for me but gave me a clue to do my own hack as follow. 我在STS jira上发现了这个线程, 线程是关于同一问题的,这使我指出了解决方案, 解决方案对我不起作用,但为我提供了以下做我自己的方法的线索。 In the build script I added following code to ignore the dependencies manually: 在构建脚本中,我添加了以下代码来手动忽略依赖项:

subprojects { 
    project.afterEvaluate {
        eclipse.classpath.file.withXml {
            it.asNode().'classpathentry'.each { entry ->
                def path = entry.@path.toLowerCase()
                if (path.contains('your-jar-to-ignore')) {
                    entry.attributes.each { attr ->
                        if (attr.attribute.@name.contains('org.eclipse.jst.component.dependency')) {
                            entry.remove(attr)
                        }
                    }
                }
            }
        }
    }
}

It hacks into the process of generating .classpath file (which is done by gradle) and prevents it of adding dependency attribute to the specified JAR. 它侵入了生成.classpath文件的过程(由gradle完成),并阻止其将依赖项属性添加到指定的JAR中。 This way it doesn't get copied to WTP deploy directory. 这样,它不会被复制到WTP部署目录。
Hope this helps people who land on this page, but I really think either Eclipse or Gradle people should fix this issue as it is very frustrating. 希望这对登陆此页面的人有所帮助,但是我真的认为Eclipse或Gradle的人都应该解决此问题,因为它非常令人沮丧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM