【问题标题】:Create a jar with external dependencies using Gradle使用 Gradle 创建具有外部依赖项的 jar
【发布时间】:2016-07-17 09:47:36
【问题描述】:

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

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

当我运行“gradle build”时,它会创建一个包含所有依赖 JAR 的 JAR 文件。但是,然后它将该 JAR 放入一个 ZIP 文件中,该文件再次包含 lib 文件夹中的依赖 jar。此 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
}

为此需要做什么?

【问题讨论】:

  • 请发布整个build.gradle 脚​​本或至少发布相关部分。看来您应用了其他 gradle 插件,这搞砸了。
  • @Opal,我已经用项目的 build.gradle 和父 build.gradle 编辑了这个问题。请检查。谢谢。
  • 我认为你可以使用 fatjar 或 shadowJar 插件。例如,github.com/musketyr/gradle-fatjar-plugin

标签: gradle


【解决方案1】:

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

是您编写了父构建文件,还是由其他人提供的?也许两个人都试图以两种不同的方式解决同一个问题?

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

【讨论】:

    猜你喜欢
    • 2020-08-19
    • 2018-06-30
    • 2014-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-06
    • 2016-04-19
    • 1970-01-01
    相关资源
    最近更新 更多