Gradle创建多项目Jar [英] Gradle create multi project Jar

查看:145
本文介绍了Gradle创建多项目Jar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,自从成立初期以来,我一直在Gradle和Android Studio上玩。但是,我发现自己在墙壁上碰到更多的时间,所以有时候值得:(。



我花了一天半的时间来解决我的目前的困境



我在哪里工作,我们使用了很多共享库项目,这意味着与Gradle的本机假设不同,我的项目并不都是嵌套在一个父项目下但是这不是我的问题)我已经得到了这个工作。



在我们的项目完成并准备好了之后,它被要求为当前的现在在旧的IntelliJ中,我将简单地生成一些JavaDocs并创建一个包含所有依赖项的Artifact,然后另一个不包含依赖关系jar并尊重它们。



然而,在Gradle这似乎是非常困难的,甚至可能不支持,我找不到任何人已经做了超过10个小时的谷歌和试验和错误后,我fi正式决定做一个演示项目来准确显示我在做什么以及我正在努力完成的事情。



我需要做一些事情。 >


  1. 生成包含所有模块依赖代码和依赖JAR文件的Jar文件

  2. 生成包含所有模块依赖代码和零依赖JAR文件

  3. 生成包含所有模块依赖代码和依赖JAR文件的AAR文件,以及如果要使用它们启动我们的Activity的资源。 / li>
  4. 生成一个包含所有模块依赖关系代码和ZERO JAR文件的AAR文件,以及如果要使用它们启动我们的Activity的资源。

所以这里有谎言我的问题。每个模块运行一个Task(类型:Jar)时,只需生成自己的代码即可。我被设法让依赖的Jar文件一次编译,但是没有常规的源代码,但是我从来没有能够得到一个Module的源代码,被包含在Jar文件中,这是我现在最大的障碍



以下是我未尝试完成此简单任务的任务列表。

  evaluationDependsOn(':dependencyModule')


任务myJar(类型:Jar){
appendix ='myJar'
from android。 sourceSets.main.allSource.files
}

任务uberJar(type:Jar){
from(configurations.compile.collect {it.isDirectory()?it:zipTree it)}){
excludeMETA-INF / *。SF
excludeMETA-INF / *。DSA
excludeMETA-INF / *。RSA

}

任务fatJar(类型:Jar,dependsOn:'compileJava'){
from {
sourceSets.main.output.classesDir
}

//添加除androi之外的所有依赖项d.jar到fat jar
从{
configured.compile.findAll {
it.getName()!='android.jar'
} .collect {
it.isDirectory()?它:zipTree(it)
}
}

archiveName ='fatJar.jar'
}

任务jarFat(类型:Jar ){
appendix =fat
from android.sourceSets.main.java
from {configurations.compile.collect {it.isDirectory()?它:zipTree(it)}}
}

任务sourcesJar(类型:Jar){
from android.sourceSets.main.allSource
classifier ='sources'
}

任务clearJar(类型:删除){
删除'build / libs / myCompiledLibrary.jar'
}

任务makeJar ('build / libs /')
include('classes.jar')
('build / libs /')
重命名('classes.jar','myCompiledLibrary.jar')
}

makeJar.dependsOn(clearJar,build)

任务jar(类型:Jar) {
from android.sourceSets.main.allJava
}

任务deployJar(类型:Jar,dependsOn:jar){
baseName = project.name +' - 部署'
deps = configurations.runtime + configured.archives.allArtifactFiles
depClasses = {deps.collect {it.isDirectory()?它:zipTree(it)}}
from(depClasses){
exclude'META-INF / MANIFEST.MF'
}
}

任务modelJar(type:Jar){
from sourceSets.main.output
}
task jarWithDependency(type:Jar){
from android.sourceSets.main.classesDir
从{configurations.compile.collect {zipTree(it)}}
}

任务androidJavadocs(类型:Javadoc){
source = android.sourceSets.main.allJava
}

没有任何工作完成。任何帮助将不胜感激!!
提前感谢您抽出时间来看这个。
如果有人想要,我有完整的功能示例项目,但是我没有看到在这里上传的选项,所以我建立的Demo项目的链接。它非常小,很容易遵循。一个类或方法基本上每个项目。



演示项目

解决方案

使用应用程序你可以通过调用distZip来获取所有库的压缩文件。默认情况下,您将获得一个 bin 目录,其中包含一个批处理文件和一个shell脚本,用于运行程序和一个 lib 目录,所有的jar文件。



您可能想要更新清单以包含所有必需的库,如下所示(我的其他内容)。



编辑我删除了对project的引用,因为在这种情况下不需要。

  jar.doFirst 
{
//将运行时所需的所有jar都聚合到局部变量(数组)中
def manifestClasspath = configuration.runtime.collect {it.name}

//删除重复的jar名称,并将数组加入单个字符串
manifestClasspath = manifestClasspath.unique()。join()

//设置清单属性 - 在此处使用mainClassName之前必须设置
manifest.attributes.put(Main-Class,mainClassName)
manifest.attributes.put (Class-Path,manifestClasspath)
}

我不是Android开发人员,所以你必须为AAR的东西添加一些额外的代码。


So I've been playing in Gradle and Android Studio now since the early days of their inception. However, I find myself banging my head on the walls far more times then it is worth at times :(.

I have spent a day and a half trying to resolve my current dilemma.

Where I work we use a lot of shared library projects. This means that unlike Gradle's native assumption, my projects are NOT all nested under one parent project. (BUT this is NOT my question) I have gotten this working.

After our projects have become complete and ready to go, it has been asked to create an SDK for the current project infrastructure for external use. Now in the old IntelliJ I would simply generate some JavaDocs and create an Artifact that includes all dependencies, then another on that does not include the dependency jars and name them respectfully.

However, in Gradle this appears to be very difficult, maybe even unsupported, and I can't find anyone else that has done it after more then 10 hours of Googling and trial and error, I finally decided to make a demo project to show exactly what I'm doing and what I'm trying to accomplish.

I need to do a few things.

  1. Generate a Jar file that includes ALL module dependency code and dependent Jar files
  2. Generate a Jar file that includes ALL module dependency code and ZERO dependent Jar files
  3. Generate an AAR file that includes All module dependency code and dependent Jar files as well as resources for launching our Activity if they want to use it.
  4. Generate an AAR file that includes All module dependency code and ZERO Jar files as well as resources for launching our Activity if they want to use it.

So there in lies my problem. Each module when running a Task with (type: Jar) simply generates it's own code. I'm managed to get the dependent Jar files to compile in once, but then there is no regular source code, but I have NEVER been able to get a Module's source code to be included in the Jar file which is my biggest hurdle right now.

Here is a list of Tasks that I have tried without accomplishing this simple task yet.

    evaluationDependsOn(':dependencyModule')


task myJar(type: Jar){
    appendix = 'myJar'
    from android.sourceSets.main.allSource.files
}

task uberJar (type: Jar){
    from(configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }) {
        exclude "META-INF/*.SF"
        exclude "META-INF/*.DSA"
        exclude "META-INF/*.RSA"
    }
}

task fatJar(type: Jar, dependsOn: 'compileJava') {
    from {
        sourceSets.main.output.classesDir
    }

    // Add all dependencies except for android.jar to the fat jar
    from {
        configurations.compile.findAll {
            it.getName() != 'android.jar'
        }.collect {
            it.isDirectory() ? it : zipTree(it)
        }
    }

    archiveName = 'fatJar.jar'
}

task jarFat(type: Jar) {
    appendix = "fat"
    from android.sourceSets.main.java
    from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
}

task sourcesJar(type: Jar) {
    from android.sourceSets.main.allSource
    classifier = 'sources'
}

task clearJar(type: Delete) {
    delete 'build/libs/myCompiledLibrary.jar'
}

task makeJar(type: Copy) {
    from('build/bundles/release/')
    into('build/libs/')
    include('classes.jar')
    rename ('classes.jar', 'myCompiledLibrary.jar')
}

makeJar.dependsOn(clearJar, build)

task jar(type: Jar) {
    from android.sourceSets.main.allJava
}

task deployJar(type: Jar, dependsOn: jar) {
    baseName = project.name + '-deploy'
    deps = configurations.runtime + configurations.archives.allArtifactFiles
    depClasses = { deps.collect { it.isDirectory() ? it : zipTree(it) } }
    from(depClasses) {
        exclude 'META-INF/MANIFEST.MF'
    }
}

task modelJar(type: Jar) {
    from sourceSets.main.output
}
task jarWithDependency(type: Jar){
    from android.sourceSets.main.classesDir
    from {configurations.compile.collect {zipTree(it)}}
}

task androidJavadocs(type: Javadoc) {
    source = android.sourceSets.main.allJava
}

Nothing has quite done the job yet. Any help would be greatly appreciated!! Thanks in advance for taking the time to look at this. I have the full functioning Sample Project if anyone would like it, but I don't see an option to upload here so is the link to the Demo project I built. It is very small and very easy to follow. One class or method basically per Project.

Demo Project

解决方案

Using the application plugin, you can just call "distZip" to get a zip with all the libraries. By default you get a bin directory with a batch file and a shell script to run the program and a lib directory with all the jar files.

You probably want to update the Manifest to include all the necessary libraries like the following (I have additional stuff in mine).

EDIT I removed the references to "project" because it should not be necessary in this instance.

 jar.doFirst
 {
     // aggregate all the jars needed at runtime into a local variable (array)
     def manifestClasspath = configurations.runtime.collect { it.name }

     // remove duplicate jar names, and join the array into a single string
     manifestClasspath = manifestClasspath.unique().join(" ")

     // set manifest attributes - mainClassName must be set before it is used here
     manifest.attributes.put("Main-Class", mainClassName)
     manifest.attributes.put("Class-Path", manifestClasspath)
 }

I'm not an Android developer, so you would have to add some additional code for the AAR stuff.

这篇关于Gradle创建多项目Jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆