欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Maven项目转Gradle,Gradle打Jar(含依赖)

程序员文章站 2022-03-03 11:51:45
...

Maven项目转Gradle项目

将Maven项目拷贝到C盘用户目录下(其他目录没试过),然后在cmd窗口中,进入Maven项目根目录下,执行gradle init --type pom 命令(可能需要在电脑上提前安装好Gradle插件),Maven项目下就会自动生成Gradle对应的配置。此时项目既是Gradle项目又是Maven项目。可用IDEA等IDE直接导入该项目。

用Gradle工具打Jar包步骤

  1. 首先,打开IDE的右侧边栏Gradle,找到:对应module -> Tasks ->other -> release,双击该task,就会执行。执行完成后,会在对应Module -> build ->libs ->lib下生成一堆jar,这些都是打驱动Jar要用到的。(这是我猜测的,嘿嘿)
  2. 然后,打开IDE的右侧边栏Gradle,找到:对应Module -> Tasks ->other -> makeAndroidJar,双击该task,就会执行。执行完成后,会在对应Module -> build ->libs下生成一个android.jar,该jar就是最后的驱动Jar。
  • makeAndroidJar task主要是将项目本身和打ethereum driver所需的所有依赖(release task 生成的jar)统一打成一个Jar;
  • 打Jar的逻辑详见makeAndroidJar task、release task 及其它相关task;
apply plugin: 'java'
apply plugin: 'idea'
jar {
    String someString = ''
    configurations.runtime.each { someString = someString + " lib//" + it.name }
    manifest {
        attributes 'Main-Class': 'com.xbs.AppMain'
        attributes 'Class-Path': someString
    }
}

//清除上次的编译过的文件
task clearPj(type: Delete) {
    delete 'build', 'target'
}

task copyJar(type: Copy) {
    from configurations.runtime
    into('build/libs/lib')
}

//把JAR复制到目标目录
task release(type: Copy, dependsOn: [build, copyJar]) {
    into('build/libs/lib')
}


task makeAndroidJar(type: Jar, dependsOn: ['build']) {
    archiveName = 'android.jar'
    from('build/classes/java/main')

    //下面这些jar需与build-> libs ->lib下所有jar 一一对应
    from(project.zipTree("build/libs/lib/xxx-api-1.0.jar"))
    from(project.zipTree("build/libs/lib//xxx-driver-1.0.jar")) 
    from(project.zipTree("build/libs/lib//xxx-util-1.0.jar")) 
    from(project.zipTree("build/libs/lib/commons-io-2.5.jar"))
    from(project.zipTree("build/libs/lib/commons-lang3-3.6.jar")) 
    from(project.zipTree("build/libs/lib/core-1.51.0.0.jar")) 
    from(project.zipTree("build/libs/lib/jackson-annotations-2.8.0.jar"))
    from(project.zipTree("build/libs/lib/jackson-core-2.8.8.jar")) 
    from(project.zipTree("build/libs/lib/jackson-databind-2.8.8.jar"))
    from(project.zipTree("build/libs/lib/javax.ws.rs-api-2.1.jar"))
    from(project.zipTree("build/libs/lib/jsr305-3.0.2.jar"))
    from(project.zipTree("build/libs/lib/nv-websocket-client-2.3.jar"))
    from(project.zipTree("build/libs/lib/okhttp-3.8.1.jar"))
    from(project.zipTree("build/libs/lib/okio-1.13.0.jar"))
    from(project.zipTree("build/libs/lib/org.eclipse.paho.client.mqttv3-1.0.3.jar"))
    from(project.zipTree("build/libs/lib/slf4j-api-1.7.22.jar"))

    destinationDir = file('build/libs')