使用Gradle创建具有外部依赖关系的jar

时间:2016-07-17 09:47:37

标签: gradle

我在build.gradle文件中添加了以下内容。

jar {
doFirst {
     into('lib') {
         from configurations.runtime
     }
 }}

当我跑步" gradle build"它创建一个包含所有依赖JAR的JAR文件。但是,然后它将JAR放入包含lib文件夹中的依赖jar的ZIP文件中。此ZIP文件结构如下所示:

-- 4.11-SNAPSHOT.jar
---- lib
----- lib/dependent1.jar
----- lib/dependent2.jar
-- lib
---- dependent1.jar
---- dependent2.jar

我不想生成ZIP。我只希望生成包含所有依赖项的单个JAR。

-- 4.11-SNAPSHOT.jar
---- lib
----- lib/dependent1.jar
----- lib/dependent2.jar

完成build.gradle如下:

repositories {
    maven {
        url 'https://repository.cloudera.com/content/repositories/releases/'
    }
    maven {
        url 'https://repository.cloudera.com/content/repositories/third-party/'
    }
}
configurations.all {
    // check for updates every build
    resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
}

dependencies {
    compile group: 'commons-collections', name: 'commons-collections', version: '3.2'
    compile group: 'com.company.projectname', name: 'domain', version: pppVersion
    compile group: 'com.company.projectname', name: 'model', version: modelVersion
    compile group: 'com.company.projectname', name: 'pipeline-util', version: pppVersion
    compile group: 'com.cloudera.crunch', name: 'crunch', version: '0.3.0-3-cdh-5.2.1'
    compile group: 'org.apache.hadoop', name: 'hadoop-common', version: '2.5.0-cdh5.3.3'
    compile group: 'org.apache.hadoop', name: 'hadoop-mapreduce-client-common', version: '2.5.0-cdh5.3.3'
    compile group: 'ch.hsr', name: 'geohash', version: '1.3.0'

    testCompile group: 'junit', name: 'junit', version: '4.+'
}
jar {
    manifest {
        attributes 'Main-Class': 'com.company.projectname.delta.DeltaClusterer'
    }
    classifier  'job'
    doFirst {
         into('lib') {
             from configurations.runtime
         }
     }
}

还有一个父build.gradle。它如下:

/**
 * Dependencies for build script plugins. Project dependencies should
 * be added further in this file.
 */
buildscript {
    repositories {
        jcenter()
    }

    dependencies {
        classpath 'com.netflix.nebula:gradle-ospackage-plugin:2.2.0'
        classpath 'org.hidetake:gradle-ssh-plugin:1.0.4'
        classpath 'be.xvrt:release-plugin:0.5.1'
    }
}

task wrapper( type: Wrapper ) {
    gradleVersion = '2.2'
}

// Release script for automated version management.
// Apply at root so it uses/updates the root gradle.properties file.
apply plugin: 'be.xvrt.release'

subprojects {

    apply plugin: 'java' // Support for Java.
    apply plugin: 'maven-publish' // Upload artifacts to Nexus.
    apply plugin: 'be.xvrt.release' // Release script for automated version management.
    apply plugin: 'org.hidetake.ssh' // Execute SSH commands.
    apply plugin: 'rpm' // Builds RPM artifacts.
    apply plugin: 'sonar-runner' // Creates Sonar reports.
    apply plugin: 'project-report' // Creates a dependency report, very useful for finding dependency conflicts.

    /**
     * Global configuration.
     */
    sourceCompatibility = 1.6
    targetCompatibility = 1.6

    compileJava.options.encoding = 'UTF-8'
    compileTestJava.options.encoding = 'UTF-8'

    /**
     * Global dependency settings for all projects. Per project dependencies
     * should be added to the build.gradle file in every project.
     */

    // Common dependencies for all projects.
    dependencies {
        // Also see resolutionStrategy below when modifying semantic-TTOM-ddct version!
        compile group: 'com.teleatlas.models', name: 'semantic-TTOM-ddct', version: '4.0.2.0'

        compile group: 'com.google.guava', name: 'guava', version: '15.0'
        compile group: 'commons-collections', name: 'commons-collections', version: '3.2.1'
        compile group: 'commons-lang', name: 'commons-lang', version: '2.6'
        compile group: 'commons-io', name: 'commons-io', version: '2.4'
        compile group: 'args4j', name: 'args4j', version: '2.0.29'
        compile group: 'log4j', name: 'log4j', version: '1.2.17'

        testCompile group: 'junit', name: 'junit', version: '4.12'
        testCompile group: 'org.mockito', name: 'mockito-core', version: '1.10.8'
    }

    // The install artifact is only required for command line applications.
    if ( !tasks.findByPath( 'war' ) ) {
        task buildInstallZip( type: Zip ) {
            from jar.outputs.files

            // Copy all dependencies into lib.
            into( 'lib' ) {
                from configurations.runtime.allArtifacts.files
                from configurations.runtime
            }
        }
    }
    /**
     * Publish to Nexus.
     */
    artifacts {
        archives buildSourcesJar
        archives buildJavadocJar

        if ( !tasks.findByPath( 'war' ) ) {
            archives buildInstallZip
        }
    }

    publishing {
        publications {
            maven( MavenPublication ) {
                if ( tasks.findByPath( 'war' ) ) {
                    from components.web
                }
                else {
                    from components.java
                }

                artifact buildSourcesJar {
                    classifier 'sources'
                }

                artifact buildJavadocJar {
                    classifier 'javadoc'
                }

                if ( !tasks.findByPath( 'war' ) ) {
                    artifact buildInstallZip {
                        classifier 'install'
                    }
                }
            }
        }
    }
}


/**
 * Build RPM artifacts for command line applications.
 */
configure( [project( ':project1' ),
            project( ':project2' ),
            project( ':project3' )] ) {
    task rpm( type: Rpm, dependsOn: 'jar' ) {
        if ( version.contains( 'SNAPSHOT' ) ) {
            logger.info( 'Building SNAPSHOT RPM.' )
            version project.version.replace( '-SNAPSHOT', ".${System.currentTimeMillis()}" )
        }
        else {
            logger.info( 'Building RELEASE RPM.' )
            version project.version
            release '1'
        }

        vendor 'company'
        packageGroup 'company'
        permissionGroup "Applications/${project.name}"

        arch NOARCH
        os LINUX

        into '/usr/share/java/'

        from( jar.outputs.files ) {
            into project.name
        }

        from( configurations.runtime ) {
            into "${project.name}/lib"
        }
    }

    // When publishing artifacts, the rpm is also published (and created).
    publish.dependsOn publishRpm
    publishRpm.dependsOn rpm
}

需要做些什么?

1 个答案:

答案 0 :(得分:0)

我猜测问题是您在父buildInstallZip文件中定义的build.gradle任务。看起来该任务正在重复现在添加到子项目中jar任务的功能。

你写过父版本文件,还是其他人提供的?也许2个人都试图以两种不同的方式解决同样的问题?

无论如何,要解决这个问题,要么禁用该子项目中的buildInstallZip任务,要么从jar任务中删除修改,而是在应用程序分发时使用buildInstallZip生成的zip。