Gradle构建可执行Jar包(转载请注明出处)

更新:

发布一个应用:

apply plugin: 'application'
mainClassName = 'com.xxx.Xxx'
这两句就可以了。

所以,如果只是想发布一个可运行的程序,完全不必看下面的文字。
 

最近开始接触Java,认识到一个似乎比Maven好用的构建工具Gradle,还来不及细细研究,但是需要构建一个可执行Jar包。上网查了一下。中文的文章千篇一律的提到一个插件apply plugin: 'executable-jar' (https://github.com/stigkj/gradle-executable-jar-plugin) ,也不知道是谁复制了谁的。

但是构建的时候不认这个插件,所以寻找其他方法。

 

用压缩软件打开Eclipse生成的可执行Jar包与Gradle构建的Jar包发现MANIFEST.MF文件内容不一样(初学Java,不知道是不是仅有这里不一样)。需要Main-ClassClass-Path两个属性。

Eclipse的Gradle插件用multiProject模板生成的项目中有这么一句

jar {
    manifest.attributes provider: 'my cool company'
}

相应内容出现在MANIFEST.MF中,所以查了一下,觉得不使用'executable-jar'插件应该一样可以解决问题,

先找到这个帖子

http://blog.csdn.net/hiarcs/article/details/6198945 (感谢作者)

大意是:

jar {
    manifest {
        attributes 'Main-Clas': 'your.Class'
        attributes 'Class-Path': 'ext.libA.jar ext.libB.jar'//添加所有依赖,用空格隔开
} }

 或查找libPath下所有Jar文件生成之:

jar {
    manifest {
        attributes 'Main-Clas': 'your.Class'
        attributes 'Class-Path': new File(libPath).list().findAll {
            it.endsWith('.jar') }.collect { "$libPath/$it" }.join(' ')
    }
}

 

由于我的绝大部分依赖是通过Gradle管理的。无法通过这种方式来生成,而且个人觉得太不灵活了。故FQ查外网,

找到这两个帖子

http://forums.gradle.org/gradle/topics/manifest_with_classpath_in_jar_task_for_subprojects

http://stackoverflow.com/questions/22659463/add-classpath-in-manifest-using-gradle

大概的方法是:

jar {
    manifest {
        attributes("Main-Class" : "com.dcx.epep.Start",
                   "Class-Path": configurations.compile.collect { it.getName() }.join(' ')))
    }
}

 这样即可以将所有被依赖的Jar文件拷贝到可执行Jar文件的目录。有人建议用runtime替换compile,查看Gradle官方文档23.5. Dependency management,runtime包含compile。故替换之。

有一个问题,如果我没有依赖,此代码会出问题(https://gist.github.com/simon04/6865179 第4个回复)

task addDependToManifest << {
    if (!configurations.runtime.isEmpty()) {
        jar.manifest.attributes('Class-Path': configurations.runtime.collect { it.name }.join(' '))
    }
}
jar.dependsOn += addDependToManifest 

 

 至此,还有一个问题,我需要将对应的被依赖的Jar文件拷贝至对应目录

http://blog.csdn.net/maosidiaoxian/article/details/24456267 这个文章说了一些方法,但是不符合我的要求。

又找到这个帖子http://www.zhihu.com/question/20760722 (感谢原回复者):

要看是什么类型的项目,如果是JEE Web工程,通过war插件就可以导出完整的war包了,可以参考Gradle的手册 如果需要把jar包copy到一个自定义的目录,可以这么写:

task copyJars(type: Copy) {
  from configurations.runtime
  into 'dist' // 目标位置
}

 Gradle的很多技巧都可以到这个Wiki找到:Cookbook - Gradle

而且我不希望是可执行Jar所在的目录。故改进之,得到我自己的代码。注意了,以下代码需要放在dependencies之后。

jar {
    manifest {
        attributes("Implementation-Title": project.name)
        attributes("Implementation-Version": version)
        attributes("Main-Class": "com.mygoogle.manager.ManageServer")
    }
}

task addDependToManifest << {
    if (!configurations.runtime.isEmpty()) {
        jar.manifest.attributes('Class-Path': ". manageer_lib/" + configurations.runtime.collect { it.name }.join(' manager_lib/'))
    }
}
jar.dependsOn += addDependToManifest 

task copyDependencies(type: Copy) {
  from configurations.runtime
  destinationDir = file('build/libs/manager_lib')
}
jar.dependsOn += copyDependencies

 最后,发现这篇文章http://pkaq.github.io/2013/10/14/buildrunnablejar/ 提供了打一个包含依赖Jar的完整包的方法:

apply plugin: 'java'
apply plugin: 'application'
mainClassName = "com.Sayer"
//仓库
repositories {
    mavenRepo url: "http://repo.maven.apache.org/maven2"
}
//依赖,为了确定依赖都打入jar包,这里随便添加了一个dom4j
dependencies {
    compile "dom4j:dom4j:1.6.1"
}
//打包
task runnbaleJar(type: Jar) {
    from files(sourceSets.main.output.classesDir)
    from configurations.runtime.asFileTree.files.collect { zipTree(it) }
    manifest {
        attributes 'Main-Class': 'com.Sayer'
    }
}
//设置编码
tasks.withType(Compile) {
    options.encoding = "UTF-8"
}

 

其他参考文章:

http://www.gradle.org/docs/current/javadoc/org/gradle/api/java/archives/Manifest.html

posted @ 2014-11-17 22:55  yongtao  阅读(8365)  评论(0编辑  收藏  举报