【问题标题】:Defining custom classpath for a jar manifest in gradle在 gradle 中为 jar 清单定义自定义类路径
【发布时间】:2012-07-13 05:52:41
【问题描述】:

我正在尝试为所有子项目(大约 30 个)定义一个 jar 任务。我尝试了以下任务:

 jar {
            destinationDir = file('../../../../_temp/core/modules')
            archiveName = baseName + '.' + extension
            metaInf {
                    from 'ejbModule/META-INF/' exclude 'MANIFEST.MF'
            }

          def manifestClasspath = configurations.runtime.collect { it.getName() }.join(',') 
            manifest {
            attributes("Manifest-Version"       : "1.0",
                "Created-By"             : vendor,
                "Specification-Title"    : appName,
                "Specification-Version"  : version,
                "Specification-Vendor"   : vendor,
                "Implementation-Title"   : appName,
                "Implementation-Version" : version,
                "Implementation-Vendor"  : vendor,
                "Main-Class"             : "com.dcx.epep.Start",
                "Class-Path"             : manifestClasspath 
            )
            }
    }

我的问题是,子项目之间的依赖关系不包含在清单的类路径中。我尝试将运行时配置更改为编译配置,但这会导致以下错误。

  • 出了什么问题:评估项目“:EskoordClient”时出现问题。

    您不能更改未处于未解决状态的配置!

这是我的 EskoordClient 项目的完整构建文件:

dependencies {     
    compile project(':ePEPClient')
}

我的大多数子项目构建文件只定义项目依赖项。超级项目的构建文件中定义了第 3 方库依赖项。

是否有可能将所有需要的类路径条目(第三方库和其他项目)包含到所有子项目的超级项目中的清单类路径中。

【问题讨论】:

  • 您是否为每个子项目声明了该任务? (我没有看到subprojects {} 块。)“无法更改配置”错误发生是因为您过早地完成工作(配置阶段而不是执行阶段)。我正确地包含了项目依赖项。您使用的是哪个 Gradle 版本?
  • 我正在使用 gradle 版本 1.0 目前我在配置中有 jar 目标 operation:configure(subprojects.findAll {it.name.endsWith('Service') || it.name.endsWith('Common') || it.name.endsWith('Client')})

标签: jar classpath manifest gradle


【解决方案1】:

这就是我让它工作的方式。仅使用调用获取项目依赖项:

getAllDependencies().withType(ProjectDependency)

然后将每个项目的 libsDir 的内容添加到我的 Class-Path 清单条目中。

jar {
    manifest {
        attributes 'Main-Class': 'com.my.package.Main'
        def manifestCp = configurations.runtime.files.collect  {
        File file = it
        "lib/${file.name}"
        }.join(' ')


         configurations.runtime.getAllDependencies().withType(ProjectDependency).each {dep->

            def depProj = dep.getDependencyProject()
            def libFilePaths = project(depProj.path).libsDir.list().collect{ inFile-> "lib/${inFile}"  }.join(' ')
            logger.info"Adding libs from project ${depProj.name}: [- ${libFilePaths} -]"
            manifestCp += ' '+libFilePaths
        }

        logger.lifecycle("")
        logger.lifecycle("---Manifest-Class-Path: ${manifestCp}")
        attributes 'Class-Path': manifestCp

    }

}

【讨论】:

  • 您可能希望从最终的类路径中删除重复项并使用 manifestCp = manifestCp.split(' ').collect().unique().sort().join(' ') 对它们进行排序...我喜欢 groovy
猜你喜欢
  • 2012-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-29
  • 1970-01-01
  • 1970-01-01
  • 2017-04-15
相关资源
最近更新 更多