我有一个gradle构建的springboot项目,这个项目依赖了很多第三方的包。现在我要将这个项目打为JAR包,但是我不想将依赖包打入jar包中。当别人引用我的jar包时,需要将jar依赖的包一起导入。请问这个怎么实现?我知道可以将jar包上传到仓库中,然后在pom中配置好依赖信息,使用maven构建的项目导入这个jar包就可以实现将依赖包一同导入,但是使用gradle构建的项目导入时,并没有将依赖的包一起导入。
1条回答 默认 最新
你知我知皆知 2024-08-08 23:27关注以下回答参考 皆我百晓生、券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。
要解决这个问题,您可以使用Gradle的
copyDependencies任务来复制特定的依赖包到目标Jar文件中。以下是如何实现此功能的示例:- 首先,在您的
build.gradle文件中添加一个名为dependencies的标签,用于定义您项目的依赖库。
dependencies { implementation 'com.example:your-library:1.0' }- 然后,创建一个新的
copyDependencies任务,并将其放置在您的build.gradle文件中的script标签中。
tasks.withType(JavaCompile) { options.encoding = "UTF-8" } // 选择要复制的依赖包和其版本 dependencies { copyDependencies from: 'classpath:{name}+{version}' } // 这里是目标文件的路径 fileTree(dir: 'target', include: '*.jar')-
在
build.gradle文件中,确保已正确设置了目标文件夹的路径。 -
最后,在
build.gradle文件中运行构建脚本以生成JAR文件。
buildscript { repositories { mavenCentral() } dependencies { classpath 'io.github.gradle.kotlinjs:kotlin-jdk-stdlib:1.6.10' } } plugins { id 'org.springframework.boot' version '2.5.5.RELEASE' id 'io.spring.dependency-management' version '1.0.11.RELEASE' id 'java' version '1.8.0_271' } group 'your.group.name' version '0.0.1-SNAPSHOT' sourceCompatibility = '1.8' runtimeCompatibility = '1.8' repositories { mavenCentral() } dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'mysql:mysql-connector-java:8.0.29' implementation 'com.fasterxml.jackson.core:jackson-databind:2.12.3' implementation 'com.google.guava:guava:28.1-jre' implementation 'org.hibernate:hibernate-core:5.4.0.Final' implementation 'org.postgresql:postgresql:42.2.15' testImplementation 'junit:junit:4.12' runtimeOnly 'org.projectlombok:lombok:1.18.20' annotationProcessor 'org.projectlombok:lombok:1.18.20' } tasks.withType<JavaCompile> { options.encoding = "UTF-8" } tasks.withType<Test> { useJUnitPlatform() } tasks.withType<Copy> { from fileTree(dir: rootProject.sourceDir, exclude: ['**/BOOT-INF/**']) into fileTree(dir: 'target', exclude: ['**/BOOT-INF/**']) } task jar(type: Jar) { from files('target/*') manifest { attributes 'Main-Class': 'YourApplication.main' } } task install(type: Copy) { from fileTree(dir: 'target', include: ['*.jar'], exclude: ['**/BOOT-INF/**']) into directory 'install' }请注意,上述示例仅适用于Spring Boot项目。如果您正在构建的是其他类型的项目,请相应调整这些设置。
解决 无用评论 打赏 举报- 首先,在您的