有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何从另一个Maven模块启动Spring Boot应用程序?

我得到了一个由两个模块组成的项目

  • 第一个是SpringBootRESTWeb服务
  • 第二个是应该用于此服务的代码

问题:我需要从代码中的另一个模块启动Web服务

当然,这里最好的选择是将服务部署到某个远程主机上,但是如果我想在本地机器上启动服务,有哪些选择呢

第一个想法是打包服务模块,然后使用maven-dependency-pluginjar复制到第二个模块,并以以下方式启动它:

Runtime.getRuntime().exec("java -jar my-rest-service.jar");

我可以从另一个模块启动Spring Boot应用程序吗?调用Application.main()方法还是什么


共 (2) 个答案

  1. # 1 楼答案

    您可以将buildinstall第一个模块作为jar库放入本地maven存储库(.m2默认文件夹)。然后,您可以在第二个模块中将该库用作maven依赖项

    之后,您可以像通常一样spring-boot启动应用程序(第二个模块)——使用main方法


    示例:

    第一个模块-库:

    <?xml version="1.0" encoding="UTF-8"?>
    <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>
    
        <url>https://example.com/module1</url>
    
        <groupId>com.example</groupId>
        <artifactId>module1</artifactId>
        <name>module1</name>
        <version>1.0.0</version>
        <packaging>jar</packaging>
    
        <properties>
            <java.version>12</java.version>
        </properties>
    
        <build>
            <finalName>module1</finalName>
            <defaultGoal>install</defaultGoal>
    
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.1</version>
                    <configuration>
                        <source>${java.version}</source>
                        <target>${java.version}</target>
                        <encoding>UTF-8</encoding>
                    </configuration>
                </plugin>
                <plugin>
                    <inherited>true</inherited>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-source-plugin</artifactId>
                    <version>3.2.1</version>
                    <executions>
                        <execution>
                            <id>attach-sources</id>
                            <goals>
                                <goal>jar</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </project>
    

    第二个模块-应用程序:

    <?xml version="1.0" encoding="UTF-8"?>
    <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>
    
        <url>https://example.com/module2</url>
    
        <groupId>com.example</groupId>
        <artifactId>module2</artifactId>
        <name>module2</name>
        <version>1.0.0</version>
        <packaging>jar</packaging>
        <! packaging>war</packaging >
    
        <properties>
            <java.version>12</java.version>
        </properties>
    
        <build>
            <finalName>module2</finalName>
            <defaultGoal>package</defaultGoal>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.1</version>
                    <configuration>
                        <source>${java.version}</source>
                        <target>${java.version}</target>
                        <encoding>UTF-8</encoding>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    
        <dependencies>
            <!  com.example  >
            <dependency>
                <groupId>com.example</groupId>
                <artifactId>module1</artifactId>
                <version>1.0.0</version>
            </dependency>
        </dependencies>
    </project>
    
  2. # 2 楼答案

    Spring Boot Maven插件将我们的应用程序打包为可执行JAR——这样的文件不能在其他项目中使用,因为类文件被放入BOOT-INF/classes

    为了与另一个项目共享类,最好的方法是创建一个包含共享类的单独jar,然后使其成为依赖于它们的所有模块的依赖项

    因此,在spring引导模块中,您需要添加如下分类器:

    ...
    <build>
      ...
      <plugins>
        ...
        <plugin>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-maven-plugin</artifactId>
          <configuration>
            <classifier>exec</classifier>
          </configuration>
        </plugin>
      </plugins>
    </build>
    

    当然,你需要在你想要使用的pom中添加你的spring引导模块依赖项,然后你应该能够使用Application.java


    请检查这里:Using a Spring Boot Application as a Dependency