如何有效地处理Gradle maven元数据?

时间:2016-08-10 20:24:29

标签: maven gradle maven-publish

以下代码使用Maven元数据创建war文件。问题是,当没有任何变化时,我没有看到与重建时相同的UP-TO-DATE消息。显然我的任务输入/输出有问题。 问题是我可以看到在没有任何实际代码更改的情况下依赖关系可能会如何变化。当没有代码实际上需要重建时,这将导致Maven元数据发生更改。不知道如何处理这个问题。请注意,我们的Jar具有相同的格式,这意味着我也必须修复它们。

我看到的例子:

...
:redacted:redacted-redacted:com-redacted:processResources UP-TO-DATE
:redacted:redacted-redacted:com-redacted:classes
:redacted:redacted-redacted:com-redacted:generatePomFileForMavenJavaPublication
:redacted:redacted-redacted:com-redacted:jar
:redacted:redacted-redacted:redacted:compileJava
:redacted:redacted-redacted:redacted:processResources UP-TO-DATE
:redacted:redacted-redacted:redacted:classes
:redacted:redacted-redacted:redacted:generatePomFileForMavenJavaPublication
:redacted:redacted-redacted:redacted:jar
:redacted:redacted-redacted:com-redacted:compileJava
:redacted:redacted-redacted:com-redacted:processResources UP-TO-DATE
:redacted:redacted-redacted:com-redacted:classes
:redacted:redacted-redacted:com-redacted:generatePomFileForMavenJavaPublication
:redacted:redacted-redacted:com-redacted:jar
:redacted:redacted-redacted:com-redacted:compileJava
:redacted:redacted-redacted:com-redacted:processResources UP-TO-DATE
:redacted:redacted-redacted:com-redacted:classes
:redacted:redacted-redacted:com-redacted:generatePomFileForMavenJavaPublication
:redacted:redacted-redacted:com-redacted:jar
:redacted:userdata-redacted:com-redacted:compileJava
:redacted:userdata-redacted:com-redacted:processResources UP-TO-DATE
:redacted:userdata-redacted:com-redacted:classes
:redacted:userdata-redacted:com-redacted:generatePomFileForMavenJavaPublication
:redacted:userdata-redacted:com-redacted:jar
:redacted:redacted-redacted:com-redacted:compileJava
:redacted:redacted-redacted:com-redacted:processResources UP-TO-DATE
:redacted:redacted-redacted:com-redacted:classes
:redacted:redacted-redacted:com-redacted:generatePomFileForMavenJavaPublication
:redacted:redacted-redacted:com-redacted:jar
:redacted:redacted-redacted:redactedservices:compileJava
:redacted:redacted-redacted:redactedservices:processResources
:redacted:redacted-redacted:redactedservices:classes
:redacted:redacted-redacted:redactedservices:generatePomFileForMavenJavaPublication
:redacted:redacted-redacted:redactedservices:war
...

有问题的Gradle代码:

apply plugin: 'war'
apply plugin: 'maven-publish'

publishing {
    publications {
        mavenJava(MavenPublication) {
            from components.java
        }
    }
}

publishing {
    repositories {
        maven {
            url "$buildDir/fauxMvnRepo"
        }
    }
}

war {

    def pomGenerationTask = project.tasks.findByName('generatePomFileForMavenJavaPublication')

    if (pomGenerationTask) {
        dependsOn 'generatePomFileForMavenJavaPublication'
        pomGenerationTask.doLast {
            createPropertiesFile()
        }

    } else {
        project.tasks.whenTaskAdded { addedTask ->
            if (addedTask.name == 'generatePomFileForMavenJavaPublication') {
                project.tasks.war.dependsOn 'generatePomFileForMavenJavaPublication'
                addedTask.doLast {
                    createPropertiesFile()
                }
            }
        }
    }

    into("META-INF/maven/$project.group/$project.archivesBaseName") {
        from new File(project.buildDir, 'publications/mavenJava')
        rename "pom-default.xml", "pom.xml"

    }

    manifest {
        attributes('Implementation-Title': project.name,
                'Implementation-Version': project.version,
                'Archiver-Version': "Gradle ${name} task",
                'Built-By': System.getProperty('user.name'),
                'Built-JDK': System.getProperty('java.version'),
                'Created-By': "Gradle",
                'Source-Compatibility': project.sourceCompatibility,
                'Target-Compatibility': project.targetCompatibility,
                'Implementation-Vendor-Id': "com.funkyChicken"
        )
    }

    doLast {
        copy {
            from destinationDir.absolutePath + File.separator + "$archiveName"
            into destinationDir
            rename { String fileName ->
                fileName.replace("${archiveName}", "${baseName}.${extension}")
            }
        }
    }
}

def createPropertiesFile() {
    File propsFile = new File("$buildDir/publications/mavenJava/pom.properties")

    def props = new Properties()
    propsFile.createNewFile()

    props.setProperty("version","$version")
    props.setProperty("groupId","$project.group")
    props.setProperty("artifactId","$project.archivesBaseName")

    props.store(propsFile.newWriter(), "#Generated by Gradle")
}

0 个答案:

没有答案