在gradle中发布带有依赖项的测试jar

时间:2014-07-24 07:00:50

标签: gradle build.gradle

我在src / integTests目录下为我的项目编写了测试,现在我必须构建和发布仅使用集成测试生成的jar。这是我的代码:

apply plugin: 'java'
apply plugin: 'maven-publish'

sourceSets {
    integTest {
        java.srcDir file('src/integTest/java')
        resources.srcDir file('src/integTest/resources')
        compileClasspath += main.output + test.output
        runtimeClasspath += main.output + test.output
    }
}

configurations {
    integTestCompile.extendsFrom testCompile
    integTestRuntime.extendsFrom testRuntime
}
task integTestJar (type:Jar) {
    from sourceSets.integTest.output
        appendix = 'integ-tests'
  }
  publishing {
      publications {
          myPublicationName(MavenPublication) {
              artifactId "client-security-internal-integ-tests"
              from components.java

          }
      }
      repositories {
          maven {
              url ="${artifactory_contextUrl}"
              credentials {
                  username = "${artifactory_user}"
                  password = "${artifactory_password}"
              }
          }
       }
   }

在上面,当我有from components.java时,它会发布产品的jar,其名称在artifact-id中指定。相反,当我使用artifact integTestJar.archivePath时,它会发布正确的jar,但不会在pom文件中包含依赖项信息。

我尝试了from components.integTest,但失败了,错误为Could not find property 'integTest' on SoftwareComponentInternal set

如何发布包含在pom文件中的所有依赖项的integ-test jar?

1 个答案:

答案 0 :(得分:2)

这是有效的解决方案!

        pom.withXml {
            def dependencies = asNode().appendNode('dependencies')
            configurations.integTestRuntime.getResolvedConfiguration().getFirstLevelModuleDependencies().each {
                def dependency = dependencies.appendNode('dependency')
                dependency.appendNode('groupId', it.moduleGroup)
                dependency.appendNode('artifactId', it.moduleName)
                dependency.appendNode('version', it.moduleVersion)
            }
        }