java打包

在maven中,指定 jar-with-dependencies ,将依赖包打进去。

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <configuration>
                <archive>
                    <manifestFile>src/main/resources/META-INF/MANIFEST.MF</manifestFile>
                </archive>
                <descriptorRefs>
                    <descriptorRef>jar-with-dependencies</descriptorRef>
                </descriptorRefs>
            </configuration>
        </plugin>
    </plugins>
</build>
gradle打包

有些情形需要将项目依赖的jar包也合并入自己项目的jar包内,出来的这个jar我们称它为fat-jar。

shadow插件:

下面例子展示将mybatis打入我的jar包,并将mybatis的包名由org.apache.ibatis 改成 my.org.apache.ibatis。

这个示例并不是一个完整的build.gradle文件。里面除了relocate这行其余均为必须。relocate:在合并包的时候修改依赖的包名。如果不是必要情况,可以忽略relocate这行。

plugins {
    id 'com.github.johnrengelman.shadow' version '2.0.4'
}
 
apply plugin: 'com.github.johnrengelman.shadow'
 
 
dependencies {
    compile group: 'org.mybatis', name: 'mybatis', version: '3.4.5'
}
 
shadowJar {
    classifier = ""
    relocate 'org.apache.ibatis', 'my.org.apache.ibatis'
    dependencies {
        include(dependency("org.mybatis:mybatis:3.4.5:withDependencies"))
    }
}

这种方式看到有的地方需要指定mainClassName,否则执行会报错:

$ ./gradlew clean build -x test
> Task :startScripts FAILED

FAILURE: Build failed with an exception.

* What went wrong:
A problem was found with the configuration of task ':startScripts'.
> No value has been specified for property 'mainClassName'.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 0s
4 actionable tasks: 4 executed

还看到这种方式

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

task multipleJar(type: Jar, dependsOn: copyJar) {
    if (!configurations.runtime.isEmpty()) {
        manifest.attributes('Class-Path': '. lib/' + configurations.runtime.collect { it.name }.join(' lib/'))
    }
    manifest {
        attributes(
                "Manifest-Version": '1.0',
                "Created-By": 'liwen',
                "Implementation-Title": project.name,
                "Implementation-Version": version,
                "Implementation-Vendor": 'fusionskye',
                'Main-Class': 'aviator.SimpleExample'
        )
    }
    baseName = project.name + '-base'
    with jar
}

这种方式没有尝试。最终用下面的方式,搞定。

plugins {
    id 'java'
}

jar {
    manifest {
        attributes(
                'Built-By'       : "xiaoyang",
                'Build-Timestamp': new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ").format(new Date()),
                'Build-Revision' : 1.0,
                'Created-By'     : "Gradle ${gradle.gradleVersion}",
                'Build-Jdk'      : "${System.properties['java.version']} (${System.properties['java.vendor']} ${System.properties['java.vm.version']})",
                'Build-OS'       : "${System.properties['os.name']} ${System.properties['os.arch']} ${System.properties['os.version']}",
                'Manifest-Version': 1.0,
                'Agent-Class': "com.agent.MyCustomAgent",
                'Can-Redefine-Classes': "true",
                'Can-Retransform-Classes': "true",
                'Premain-Class': "com.agent.MyCustomAgent"
        )
    }

    from {
        configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
    }
}

group 'com.xiaoyang'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    compile 'org.javassist:javassist:3.25.0-GA'
    testCompile group: 'junit', name: 'junit', version: '4.12'
}



打包成功后再运行
java -javaagent:/Users/usename/github/java/java_agent_demo/build/libs/java_agent_demo-1.0-SNAPSHOT.jar PersonTest

在这里插入图片描述

参考文档:
1.https://www.cnblogs.com/2333/p/9569631.html
2.https://blog.csdn.net/carson2440/article/details/82217461
3.https://blog.csdn.net/u011990675/article/details/90768940

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值