【问题标题】:How to create spring boot project from existing maven project如何从现有的 Maven 项目创建 Spring Boot 项目
【发布时间】:2023-03-04 14:53:01
【问题描述】:

我在 eclipse 中创建了一个 maven 项目,我想将它部署在服务器上(我猜是 tomcat)。我相信这可以通过弹簧靴来完成。 我的问题是:这个项目已经导入了几个位于我的 PC 上的 jar 文件,并且在其构建路径的工作区中有另一个项目。我担心在集成过程中(把它变成 maven)我可能会失去所有的依赖。将 jar 文件导入项目的最佳方式通常是什么,以及如何在其构建路径中添加另一个项目?请评论您需要的任何其他规格。

这是 pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.apache.jena</groupId>
  <artifactId>jena-examples</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>Apache Jena - Code Examples</name>
  <description>A collection of example code illustrating uses of Apache Jena</description>
  <url>http://jena.apache.org/</url>

  <properties>
    <ver.jena>[3.1.0,)</ver.jena>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <organization>
    <name>Apache Software Foundation</name>
    <url>http://apache.org</url>
  </organization>

  <dependencies>
    <dependency>
      <groupId>org.apache.jena</groupId>
      <artifactId>apache-jena-libs</artifactId>
      <version>${ver.jena}</version>
      <type>pom</type>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.10</version>
      <type>jar</type>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>commons-cli</groupId>
      <artifactId>commons-cli</artifactId>
      <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>com.mashape.unirest</groupId>
        <artifactId>unirest-java</artifactId>
        <version>1.4.9</version>
    </dependency>
    <dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpclient</artifactId>
      <version>4.3.6</version>
    </dependency>
    <dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpasyncclient</artifactId>
      <version>4.0.2</version>
    </dependency>
    <dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpmime</artifactId>
      <version>4.3.6</version>
    </dependency>
    <dependency>
      <groupId>org.json</groupId>
      <artifactId>json</artifactId>
      <version>20140107</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.8</version>
    </dependency>
    <dependency>
     <groupId>org.glassfish.jersey.core</groupId>
     <artifactId>jersey-client</artifactId>
     <version>2.8</version>
 </dependency>
 <dependency>
     <groupId>org.glassfish.jersey.media</groupId>
     <artifactId>jersey-media-json-jackson</artifactId>
     <version>2.8</version>
 </dependency>
 <dependency>
        <groupId>com.github.galigator.openllet</groupId>
        <artifactId>openllet-owlapi</artifactId>
        <version>2.6.4</version>
    </dependency>
    <dependency>
        <groupId>com.github.galigator.openllet</groupId>
        <artifactId>openllet-jena</artifactId>
        <version>2.6.4</version>
    </dependency>
  </dependencies>

</project>

【问题讨论】:

    标签: java eclipse maven spring-boot


    【解决方案1】:

    要在 maven 项目中集成本地 jar 文件,请查看: How to add local jar files to a Maven project?

    要将 Spring Boot 添加到您的 pom.xml 中,您需要添加以下内容 (from this doc

    <!-- Inherit defaults from Spring Boot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
    </parent>
    
    <!-- Add typical dependencies for a web application -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    
    <!-- Package as an executable jar -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    

    【讨论】:

    • 我确实配置了我的 pom,我认为 spring 构建正确,但我的 jar 实在太多,无法单独添加它们。当 jar 本地存储在我的 PC 上时,我仍然可以让我的应用程序在 centos 7 VM tomcat 服务器上运行吗?
    • 我想你可以通过几个步骤来做到这一点: 1 - 将你的 spring 应用程序打包为 war 2 - 将本地 jar 存储在你的 VM 上的文件夹挂载 3 - 使用挂载的文件夹,以便能够解决依赖关系。尽管我强烈建议您将本地依赖项存储在应用程序基础存储库中的 /bin 文件夹中,然后使用 maven 添加。重现性更强。
    • 我还建议通过将您的应用程序打包为 jar 文件来使用 SpringBoot 嵌入式 Tomcat。如果在 pom.xml 中指定了所有本地依赖项,则只要安装了 java,就可以生成应用程序的 jar 文件并将其部署到所需的任何位置。
    • “您将本地依赖项存储在应用程序基础存储库的 /bin 文件夹中,然后使用 maven 添加”是什么意思?我不能像往常一样将它们添加到构建路径吗?我需要对罐子做一些特定的事情吗?
    • 我理解了关于在项目内的 lib 文件夹中 sroring 它们的部分,但我将它们添加到正常构建路径中。
    【解决方案2】:

    我认为您对 spring.boot 的实际含义有点误解。

    Spring-boot 是一个完整的生态系统,用于构建微服务,集成了许多使用良好的 java 库。

    你的 pom 接缝是一个完全编写的应用程序,可以部署到一个空的 tomcat 服务器上。 Spring-boot 不是一个空的 tomcat,spring-boot 是一个在底层使用 tomcat 的应用程序/框架。

    您需要转到http://tomcat.apache.org/ 并下载 tomcat 服务器,然后在您选择的 IDEA 中搜索如何设置/启动 tomcat。完成后,您需要在您选择的 IDEA 中打开 maven 项目并将其部署到 tomcat 服务器。

    使用 eclipse/intellij 互联网上有几个教程解释了如何安装和设置 tomcat。还有大量关于如何导入 maven 项目以及构建和部署到 tomcat 的教程。

    【讨论】:

    • 空是什么意思?我已经在 centos 7 VM 上安装了 tomcat,我希望我的 java 应用程序在该服务器上运行。我还担心的是存储在我的 PC 中某处的所有 jar 以及如何将它们“上传”并与 java 程序的其余部分集成,而不会出现任何我希望能够发送和接收请求的错误在服务器上以及在同一工作区中的另一个项目上,我已经建立了使用该项目代码的路径。正如最佳答案所暗示的那样,我确实添加了弹簧靴,但由于罐子太多,我没有这样做。
    • 当你使用 maven 构建东西时,你的 pom.xml 中所有指定的依赖项都将捆绑在最终生成的 jar 中。所以运行“mvn clean install”会产生一个目标文件夹。在该文件夹中,您将拥有一个 jar,它是您的程序,其中包含您的代码,以及在 pom.xml 中定义的所有捆绑 jar。这个 jar 需要部署到你的 tomcat 服务器上。我建议你查一个 maven 教程,然后在线部署到 tomcat 教程。
    • tomcat 是一个应用服务器。这意味着您可以将应用程序部署到它。所以你有一个空的 tomcat 服务器正在运行。然后你将你的应用程序部署到它(你的 jar)
    • 这里有几种不同的方式将应用程序部署到 tomcat baeldung.com/tomcat-deploy-war
    • 另一个问题。我在我的 VM 中安装了 Java,但我还需要安装 maven 吗?而且当我使用 maven 构建我的项目时,我必须运行哪些命令以及按什么顺序运行:mvn compile?证实?安装?干净安装?初始化?我猜 mvn 包是最后一个。
    猜你喜欢
    • 2018-02-23
    • 2020-04-01
    • 2018-10-02
    • 1970-01-01
    • 2023-01-28
    • 1970-01-01
    • 1970-01-01
    • 2018-05-21
    • 1970-01-01
    相关资源
    最近更新 更多