在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