簡體   English   中英

如何從另一個 Maven 模塊啟動 Spring 引導應用程序?

[英]How to launch Spring Boot app from another Maven module?

我有一個由兩個模塊組成的項目。

  • 第一個是 Spring 啟動 REST Web 服務
  • 第二個是應該與此服務一起使用的代碼。

問題:我需要從代碼中的另一個模塊啟動 Web 服務。

當然,這里最好的選擇是將 Service 部署到某個遠程主機,但是如果我想在本地機器上啟動 Service,有哪些選擇呢?

第一個想法是打包服務模塊,然后使用maven-dependency-pluginjar復制到第二個模塊並啟動它:

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

我可以直接從另一個模塊啟動 Spring 啟動應用程序嗎? 調用Application.main()方法還是什么?

您可以將第一個模塊作為jarbuildinstall到本地 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>

The Spring Boot Maven plugins package our application as executable JARs – such a file can't be used in another project since class files are put into 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


請在此處查看:使用 Spring 引導應用程序作為依賴項

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM