【问题标题】:Create Executable jar file from Gradle and import jar in maven从 Gradle 创建可执行 jar 文件并在 maven 中导入 jar
【发布时间】:2019-10-06 10:31:34
【问题描述】:

你好,我创建了一个带有 gradle 配置的 spring boot 项目,其中包含身份验证、社交登录等常见功能,我想制作可执行的 jar 文件并作为 gradle 项目的依赖项,以便我可以将其导入另一个 spring gradle 或 maven 项目为了访问包含依赖项的 REST 端点

(项目A:想作为库)

build.gradle

plugins {
    id 'org.springframework.boot' version '2.1.5.RELEASE'
    id 'java'
}
apply plugin: 'io.spring.dependency-management'

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
    mavenCentral()
}

springBoot.mainClassName = "com.example.auth"
bootJar.enabled = true

jar {
    manifest {
        attributes 'Main-Class': 'com.example.auth'
    }
}
dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

项目还包含像

这样的端点
package com.example.auth;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/test/")
public class TestController {

    @GetMapping("testCalled")
    public String testCalled(){
        return "test called..";
    }
}

它使用 gradle build 命令创建了一个 jar 文件,并使用以下命令作为依赖项安装到另一个项目中:

mvn install:install-file "-Dfile=F:\auth\build\libs\auth-0.0.1-SNAPSHOT.jar" -DgroupId=com.auth -DartifactId=test-authentication -Dversion=1.0 -Dpackaging=jar

它还成功安装了依赖项

项目 B:Spring Boot Maven 项目

我想使用休息端点

/api/test/testCalled

来自maven项目(项目B)

有什么想法请帮忙!!

谢谢。

【问题讨论】:

  • 你不想要一个可执行的 jar,你想要/需要一个普通的 jar。 Spirng Boot 将 jar 重新打包为不同的结构,Spring Boot jar 不能用作依赖项。如果这不是一个独立的应用程序,不要创建一个可执行的 jar,而是一个常规的,否则创建 2 个工件,如 here 所述
  • @M.Deinum 我也尝试使用常规 jar,如果我将库项目创建为 maven,但相同的功能在 gradle 项目中不起作用
  • 您的 gradle 项目正在创建一个 spring boot jar 而不是常规 jar。移除 spring-boot 插件,只使用依赖插件。
  • 但是如果我创建了一个普通的 jar,那么它也不起作用我创建了 spring boot jar 仅用于测试目的,
  • 什么不起作用?请说清楚?只需添加一个 jar 可以就足够了,但是如果你的包没有被默认的 @ComponentScan 覆盖,它显然不会工作。

标签: spring-boot gradle jar


【解决方案1】:

为了实现这一点,您应该包含以下 gradle 部分:

jar {
    manifest {
        attributes 'Main-Class': 'com.aaa.bbb.SpringBootApplication'
    }
}

【讨论】:

  • 添加您的 build.gradle 文件以及您在尝试构建项目时收到的堆栈跟踪
猜你喜欢
  • 1970-01-01
  • 2018-07-24
  • 2020-05-20
  • 2020-04-10
  • 1970-01-01
  • 1970-01-01
  • 2017-11-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多