在使用MacOS运行SpringBoot应用程序方面遇到了很大的困难(而不是通过IDE运行)

由于在Mac OS上直接运行Spring Boot应用程序非常困难,所以我将其作为备忘录进行记录。

<什么是问题?>
在IDE(IntelliJ)上构建了Spring Boot应用程序并通过IntelliJ启动以运行Web应用程序,这是可以实现的。
但是,当我直接从操作系统访问该文件夹(而不是通过IDE)并尝试使用Javac命令进行构建时,会出现错误,无法构建。
原因是,通过IDE运行时,利用了IDE本身的各种功能,而直接从操作系统操作时,这些功能无法使用,因此需要进行一些调整或采用不同的方法。

<怎么办好呢?> 这里有另一种方法。
→ https://qiita.com/hikarut/items/d1273f99e5b124a887bd
上面的网站几乎完美,但至少在我的环境中,如果不设置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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>edu.self</groupId>
  <artifactId>my_first_spring_boot</artifactId>
  <packaging>jar</packaging>
  <version>1.0.0-SNAPSHOT</version>
  <name>my_first_spring_boot</name>
  <url>http://maven.apache.org</url>
  <!-- Spring Boot の利用を宣言し、バージョンを指定【追加】 -->
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.6.RELEASE</version>
  </parent>
  <dependencies>
    <!-- Spring Boot の Web アプリケーションライブラリの利用を指定【追加】 -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- Spring Boot の ユニットテストライブラリの利用を指定【追加】 -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
<!-- JUnitの指定を削除(Spring Boot に同梱されているため)【削除】
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
 -->
  </dependencies>
<build>
<plugins>
 <plugin>
  <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-maven-plugin</artifactId>
         <configuration>
            <skip>true</skip>
         </configuration>
         <executions>
            <execution>
               <phase>none</phase>
            </execution>
         </executions>
      </plugin>
   </plugins>
</build> 
 <properties>
    <!-- Java バージョン指定【追加】 -->
    <java.version>1.8</java.version>
  </properties>
</project>

需要在上述网站上更改 plugins 部分的设置如下。

<build>
    <plugins>
      <!-- Spring Boot の ビルド用 Maven プラグイン【追加】 -->
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>

在进行修正后的情况下,运行下述命令将会起作用。
但是,请注意,下述命令需要在包含pom.xml文件的文件夹中运行(否则可能会出现以下错误:https://stackoverflow.com/questions/30855864/maven-no-plugin-found-for-prefix-spring-boot-in-the-current-project-and-in-th)

在包含pom.xml文件的文件夹中执行。

$ mvn spring-boot:run

行动结果

testtest@testtest-mac my_first_spring_boot % ls
pom.xml	src	target
testtest@testtest-mac my_first_spring_boot % mvn spring-boot:run
[INFO] Scanning for projects...
[INFO] 
[INFO] -------------------< edu.self:my_first_spring_boot >--------------------
[INFO] Building my_first_spring_boot 1.0.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] >>> spring-boot-maven-plugin:1.5.6.RELEASE:run (default-cli) > test-compile @ my_first_spring_boot >>>
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ my_first_spring_boot ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/yonishik_jp/my_first_spring_boot/src/main/resources
[INFO] skip non existing resourceDirectory /Users/yonishik_jp/my_first_spring_boot/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ my_first_spring_boot ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /Users/yonishik_jp/my_first_spring_boot/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ my_first_spring_boot ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/yonishik_jp/my_first_spring_boot/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ my_first_spring_boot ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] <<< spring-boot-maven-plugin:1.5.6.RELEASE:run (default-cli) < test-compile @ my_first_spring_boot <<<
[INFO] 
[INFO] 
[INFO] --- spring-boot-maven-plugin:1.5.6.RELEASE:run (default-cli) @ my_first_spring_boot ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.944 s
[INFO] Finished at: 2022-12-11T22:41:23+09:00
[INFO] ------------------------------------------------------------------------
testtest@testtest-mac my_first_spring_boot % 
广告
将在 10 秒后关闭
bannerAds