Gradle打包

1.打包普通jar依赖包

apply plugin: 'java'

////////依赖设置////////
dependencies {
    compile group: 'org.jboss.netty', name: 'netty', version: '3.2.7.Final'
}

////////测试设置////////
task testAll() {
    //命令行执行, 在gradle的runtime下执行测试(保证所有的包已被引用)
    doLast {
        javaexec {
            main = "org.junit.runner.JUnitCore"; args = ['com.test.tests.AllTests']
            classpath configurations.runtime + sourceSets.main.output + sourceSets.test.output
            workingDir("../")
        }
    }
}
//跳过自带测试, 全部采用testAll的命令行方式(自带的无法针对单类测试)test {
    exclude '**'
}

2.打包可执行jar包

apply plugin: 'java'

////////依赖设置////////
dependencies {
    compile group: 'org.perf4j', name: 'perf4j', version: '0.9.16'
}

jar {
    from {
        (configurations.runtime).collect {
            it.isDirectory() ? it : zipTree(it)
        }
    }
    //此行是当出现Invalid signature file digest for Manifest main attributes错误时添加
    exclude 'META-INF/*.RSA', 'META-INF/*.SF','META-INF/*.DSA'

    manifest {
        attributes(
          'Main-Class': 'com.yummy.pressure.entry.PressureServer'
        )
    }
}

////////测试设置////////
//跳过测试
test {
    exclude '**'
}

3.打包war包

build.gradle

apply plugin: 'java'
apply plugin: 'war'

////////依赖设置////////
dependencies {
    compile project(':test-service')
    compile project(':test-common')

    compile group: 'org.perf4j', name: 'perf4j', version: '0.9.16'
    compile group: 'log4j', name: 'log4j', version: '1.2.17'
}

////////测试设置////////
//跳过测试
test {
    exclude '**'
}

setting.gradle

所有要编译的包

include "test-common", "test-service", "test-web"

4.发布打包好的包

apply plugin: 'java'
apply plugin: 'idea'

////////子项目公用配置
subprojects {
    apply plugin: 'idea'
    apply plugin: 'java'


    idea {
        module {
            downloadSources = true
        }
    }

    //中央库设置
    repositories {
        //本地中央库
        mavenLocal()
        //远程中央库
        mavenRepo name: 'Public Repositories', url: "http://192.168.161.26:8080/nexus/content/groups/NewYork-public/"
    }
}

//部署所有代码到内网开发机器
task deployAll(dependsOn: ["deployToolDev", "deployDev"]) {
    doLast {
    }
}

//部署tool代码到内网开发机器
task deployToolDev(dependsOn: ["test-tool-web:build"]) {
    doLast {
        //部署代码
        exec {
            executable "sh"
            args "-c", "rsync  -ztrvlC --delete test-tool-web/build/libs/test-tool-web.war " +
                    "root@192.168.161.46:/opt/local/jetty/standalone/testToolDev/webapps/test-tool-dev.war"
        }

        // 远程执行
        exec {
            executable "sh"
            args "-c", "ssh root@192.168.161.46 /opt/local/jetty/standalone/testToolDev/server.sh switch"
        }
    }
}

//部署game web代码到内网开发机器
task deployDev(dependsOn: ["test-web:build"]) {
    doLast {
        //部署代码
        exec {
            executable "sh"
            args "-c", "rsync  -ztrvlC --delete test-web/build/libs/test-web.war " +
                    "root@192.168.161.46:/opt/local/jetty/standalone/testDev/webapps/test-dev.war"
        }

        // 远程执行
        exec {
            executable "sh"
            args "-c", "ssh root@192.168.161.46 /opt/local/jetty/standalone/testDev/server.sh switch"
        }
    }
}

//部署代码到hudson, 并执行所有测试
task deployHudson(dependsOn: ["deployHudson_sql", "deployHudson_config"]) {
    doLast {
        /****************************tool**********************/
        //部署代码
        exec {
            executable "sh"
            args "-c", "rsync  -ztrvlC --delete test-tool-web/build/libs/test-tool-web.war root@192.168.161.17:/opt/local/jetty/standalone/testToolHudson/webapps/game.war"
        }

        //远程执行
        exec {
            executable "sh"
            args "-c", "ssh root@192.168.161.17 /opt/local/jetty/standalone/testToolHudson/server.sh switch"
        }

        //恢复配置连接服务器为默认
        exec {
            executable "sh"
            args "-c", "git checkout -- test-tool-web/src/main/webapp/WEB-INF/config-manager-info.xml"
        }

        /****************************web**********************/
        //部署代码
        exec {
            executable "sh"
            args "-c", "rsync  -ztrvlC --delete test-web/build/libs/test-web.war root@192.168.161.17:/opt/local/jetty/standalone/testHudson/webapps/game.war"
        }

        //远程执行
        exec {
            executable "sh"
            args "-c", "ssh root@192.168.161.17 /opt/local/jetty/standalone/testHudson/server.sh switch"
        }

        //恢复配置连接服务器为默认
        exec {
            executable "sh"
            args "-c", "git checkout -- test-web/src/main/webapp/WEB-INF/config-manager-info.xml"
        }

        //执行所有测试
        project(":test-tool-web").tasks.testAll.execute()
        project(":test-idworker-web").tasks.testAll.execute()

        //等待10秒
        exec {
            executable "sh"
            args "-c", "sleep 10"
        }

        project(":test-service").tasks.testAll.execute()
    }
}

task deployHudson_sql(dependsOn: ["test-tool-web:build", "test-web:build"]) {
    doLast {
        //部署数据库
        exec {
            executable "sh"
            args "-c", "mysql -uroot -pabcdefg -h1192.168.161.16 --secure_auth=off test_tool_hudson < test-tool-web/src/main/webapp/WEB-INF/db.sql"
        }
    }

    doLast {
        //部署数据库
        exec {
            executable "sh"
            args "-c", "mysql -uroot -pabcdefg -h192.168.161.16 --secure_auth=off test_hudson < test-web/src/main/webapp/WEB-INF/db.sql"
        }
    }
}

task deployHudson_config() {
    doLast {
        //使用tool hudson配置文件
        exec {
            executable "sh"
            args "-c", "cp -rf test-tool-web/src/main/webapp/WEB-INF/config-manager-info-hudson.xml test-tool-web/src/main/webapp/WEB-INF/config-manager-info.xml"
        }

        //使用idworker hudson配置文件
        exec {
            executable "sh"
            args "-c", "cp -rf test-idworker-web/src/main/webapp/WEB-INF/config-manager-info-hudson.xml test-idworker-web/src/main/webapp/WEB-INF/config-manager-info.xml"
        }

        //使用web hudson配置文件
        exec {
            executable "sh"
            args "-c", "cp -rf test-web/src/main/webapp/WEB-INF/config-manager-info-hudson.xml test-web/src/main/webapp/WEB-INF/config-manager-info.xml"
        }
    }
}

5.shell给gradle传参

#传参使用-P参数,P后面是key=value格式,即gradle脚本里使用的变量名称
gradle deploy${env} -Pauthor=$HUDSON_USER --info

 

### 使用 Gradle 打包 Python 项目 通常情况下,Gradle 主要用于 Java 和 Android 项目的构建管理。然而,通过自定义配置也可以让 Gradle 支持其他类型的项目,比如 Python 项目。 为了使 Gradle 能够处理 Python 项目,需要创建一个特定的任务来执行打包操作。这可以通过编写 Groovy 或 Kotlin DSL 的脚本来完成,在此提供基于 Groovy 的解决方案: #### 创建 `build.gradle` 文件 在 Python 项目的根目录下新建名为 `build.gradle` 的文件,并添加如下内容: ```groovy plugins { id 'base' } task buildPyApp(type: Exec) { workingDir './src' // 假设源码位于 src 目录中 commandLine 'pyinstaller', '--onefile', '-w', 'main.py' // 修改为你自己的入口文件名 } ``` 上述代码片段定义了一个简单的任务 `buildPyApp` 来运行 PyInstaller 工具进行单文件应用的构建[^1]。 对于更复杂的场景,如果希望集成 Gradle 构建过程与 Python 的虚拟环境或其他工具链,则可能还需要进一步扩展该脚本的功能。 另外一种方法是借助于外部命令行接口(CLI),像引用中的例子那样使用 Python 自带模块或第三方库配合 Gradle 完成打包工作。例如,可以采用 `subprocess` 模块调用 Gradle 进行 Android 应用程序的编译和组装[^3]: ```python import subprocess def call_gradle(): ret = subprocess.call("gradle -p path_to_android_project assembleDebug", shell=True) return ret == 0 ``` 需要注意的是,这种方法适用于那些已经在本地安装好所需依赖项的情况;而对于 CI/CD 流水线或者其他自动化环境中,建议优先考虑直接在 Gradle 中声明相应的插件和支持功能。 最后提醒一点,虽然理论上可行,但在实际生产环境中很少见到有人会这样做——即用 Gradle 来管理和打包纯 Python 项目。大多数开发者会选择更适合的语言原生工具如 setuptools、poetry 或者 pipenv 等来进行此类任务。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值