3
1

More than 1 year has passed since last update.

MacOSからSpringBootアプリを動かすのに凄い苦労した話(IDE経由で動かすのではなく)

Last updated at Posted at 2022-12-11

Spring BootアプリをMac OS上で直接、動かすのに凄い苦労したので、備忘録的に記載しておきます。

<何が課題か?>
IDE(IntelliJ)上で、Spring Bootアプリを構築して、IntelliJ経由で起動して、Webアプリケーションを動かす事はできた。
https://www.tairaengineer-note.com/intellij-idea-springboot-hello-world/
https://pleiades.io/help/idea/your-first-spring-application.html#create-new-spring-boot-project
ただ、このフォルダーにOSから直接アクセして(IDE経由ではなく)、Javacコマンドでビルドしようとしても、エラーが発生して、ビルドできない。
理由は、IDE経由で動かす場合は、IDE自体の機能を各種利用しているからであって、OSから直接叩くと、それらの機能が使えないので、一工夫というか、別のアプローチが必要となる。

<どうすれば良いか?>
その別のアプローチがここ。
https://qiita.com/hikarut/items/d1273f99e5b124a887bd
上のサイトがほぼ完璧だが、少なくとも私の環境では以下の様に、pom.xmlを設定しないと、エラー発生して、runできなかった。

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の部分が変更する必要があった。

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

修正した状態で、下記をRunすると動作します。
ただ、下記のコマンドは、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 % 
3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1