经过数小时的搜索和研究,我想出了一个工作示例,用于在使用Gradle 1.11和maven-publish插件时将正确的Maven依赖项写入生成的pom.xml中。
我遇到的第一个问题是所提供的依赖关系,该依赖关系始终在运行时写入pom.xml中
第二个问题是动态版本,我用于较小的版本更改。 Maven和Gradle具有不同的符号,并且maven-publish只是将Gradle类型的符号写入pom.xml中。
这是我的示例:
apply plugin: 'java'
apply plugin: 'maven-publish'
group = 'de.pentos'
version = '0.4.4'
sourceCompatibility = 1.7
repositories {
mavenCentral()
mavenLocal()
}
configurations {
provided
compile.extendsFrom provided
}
dependencies {
provided("org.projectlombok:lombok:1.+")
provided("javax.servlet:javax.servlet-api:3.1.0")
compile("org.slf4j:slf4j-api:1.7+")
compile("com.fasterxml.jackson.core:jackson-databind:2.3.+")
compile("joda-time:joda-time:2.3+")
compile("org.springframework:spring-webmvc:3.2+")
compile("org.springframework.security:spring-security-web:3.1+")
testCompile("junit:junit:4.11")
}
jar { baseName = "${project.group}.${project.name}" }
publishing {
publications {
jar(MavenPublication) {
from components.java
artifactId "${project.name}"
artifact sourceJar { classifier "sources" }
pom.withXml {
final Node root = asNode()
final versionPattern = ~/(?:(.+)\.)?(.+?)\.?\+/
configurations.compile.allDependencies.each {
final name = it.name
final group = it.group
final m = versionPattern.matcher(it.version)
if (m.matches()) {
final base = m[0][1]
final rest = m[0][2].toInteger()
final version = '[' + (base ? base + '.' : '') + (rest) + ',' + (base ? base + '.' : '') + (rest + 1) + ')'
root.dependencies.first().findAll{
it.groupId.first().value()[0] == group && it.artifactId.first().value()[0] == name
}.each {
it.version.first().value = version
}
}
}
configurations.provided.allDependencies.each {
final name = it.name
final group = it.group
root.dependencies.first().findAll{
it.groupId.first().value()[0] == group && it.artifactId.first().value()[0] == name
}.each {
it.scope.first().value = 'provided'
}
}
}
}
}
}
tasks.withType(Compile) { options.encoding = 'UTF-8' }
task sourceJar(type: Jar) {
classifier = 'sources'
from sourceSets.main.allSource
}
那我的问题是什么?
我遇到了很多比较简单的例子的麻烦
..findAll {
it.groupId == group
}
这对我没有用,我不得不通过大量测试找出如何使
findAll
起作用。那我的版本详细吗?可以用更少的代码编写吗?
我可以将依赖关系管理的功能提取到更具系统范围的脚本中,该脚本可以从需要此依赖关系管理的所有其他项目中重用吗?怎么样?
在compileJava期间,我得到警告
:compileJavawarning: [options] bootstrap class path not set in conjunction with -source 1.7
这是什么意思,我该如何解决。
最佳答案
I had some trouble with a lot examples that simply compared [...]
it.groupId
不是字符串。您将需要类似it.groupId.value()
或it.groupId[0].value()
的东西。Can I extract the functionality for dependency management into a more system wide script, that can be reused from all other projects, that need this dependency management? How?
您可以编写一个插件类,然后将其作为Jar交付。有关详细信息,请参见Gradle User Guide。
During compileJava I get the warning :compileJavawarning: [options] bootstrap class path not set in conjunction with -source 1.7 What does this mean, and how can I fix that.
这个问题已经被问过很多次了。参见Stack Overflow或http://forums.gradle.org。
PS:不必从一开始就将Ivy重写为Maven版本范围语法,应该可以从一开始就使用Maven语法(即在构建脚本中)。
关于java - 从Gradle创建正确的Maven依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22915179/