简体   繁体   English

添加到子项目到gradle jar

[英]Add to subproject to gradle jar

How can I add a subproject referenced using project(':api') to the jar gradle builds? 如何将使用project(':api')引用的子项目添加到jar gradle构建中?

This is the build.gradle of my main project. 这是我的主项目的build.gradle The subproject is includes as git submodule and has a similar buildscript. 子项目包含为git子模块,并具有类似的buildcript。

apply plugin: 'java'

sourceCompatibility = 1.5
version = '1.0'

jar {
    manifest {
        attributes('Main-Class': '..........')
    }
}

repositories {
    mavenCentral()
}

dependencies {
    compile files('libs/jfxrt.jar')
    compile project(':api')
    testCompile group: 'junit', name: 'junit', version: '4.11'
}

I figured it out on my own. 我自己想出来了。

Include the source of a subproject in the main jar: 在主jar中包含子项目的源:

sourceSets {
    main {
        java {
            srcDir project(':api').file('src/main/java')
        }
    }
}

Including the classes of a jar in the main jar: 在主jar中包含jar的类:

jar {
    from zipTree('libs/abc.jar')
}

Try to add classpath to your manifest file. 尝试将classpath添加到清单文件中。 You need to have directory (example below uses "lib") to keep jar files on which your project depends. 您需要有目录(下面的示例使用“lib”)来保存项目所依赖的jar文件。

Try modifying your "jar" block in gradle build to something like this. 尝试将gradle build中的“jar”块修改为类似的东西。 I have some addition properties just for demonstration. 我有一些额外的属性只是为了演示。 But the important one is Class-Path 但重要的是Class-Path

jar {
    manifest.attributes(
        'Class-Path': lib/api.jar
        'Built-By': System.getProperty('user.name'),
        'Built-JDK': System.getProperty('java.version'),
        'Built-OS': System.getProperty('os.name'),
        'Built-DATE': buildDate,
    )
}

I hope it helps to fix your issue. 我希望它有助于解决您的问题。

In the simplest case, a fat Jar can be created as follows: 在最简单的情况下,可以创建一个胖Jar,如下所示:

main/build.gradle: 主/的build.gradle:

jar {
    from configurations.runtime
}

There are other, more robust solutions, such as the gradle-one-jar plugin for "main" method style applications. 还有其他更强大的解决方案,例如用于“主要”方法样式应用程序的gradle-one-jar插件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM