kdnakt blog

hello there.

PowerShellでmavenコマンド実行時にシステムプロパティが認識されない

spring.io

Spring Frameworkを利用したアプリケーションを開発している(正確にはSpring Bootだ)。諸事情により、開発にはEclipseベースのSpring Tool Suite 3を利用していたが、公式サイトではVS Codeにも対応したSpring Tools 4が案内されて久しい。JavaScriptPythonなどJava以外の開発ではVS Codeを使う機会も増えてきたので、思い切ってVS CodeJavaアプリケーション開発を始めたところ、思わぬところで詰まったのでメモしておく。

 

 

[システムプロパティでmvn installがエラー終了]

VS CodePowerShell上で、他のMavenプロジェクト同様に、mvn installコマンドに-Dskip.yarnを追加の引数として設定して実行したところ、次のようにエラーが発生した。

f:id:kidani_a:20191009024904p:plain

PS C:\sandbox\mvntest\sample> mvn install -Dskip.yarn
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building sample 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.166 s
[INFO] Finished at: 2019-10-XXTXX:XX:XX+09:00
[INFO] Final Memory: 4M/15M
[INFO] ------------------------------------------------------------------------
[ERROR] Unknown lifecycle phase ".yarn". You must specify a valid lifecycle phase or a goal in the format : or :[:]:. Available lifecycle phases are: validate, initialize, generate-sources, process-sources, generate-resources, process-resources, compile, process-classes, generate-test-sources, process-test-sources, generate-test-resources, process-test-resources, test-compile, process-test-classes, test, prepare-package, package, pre-integration-test, integration-test, post-integration-test, verify, install, deploy, pre-clean, clean, post-clean, pre-site, site, post-site, site-deploy. -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/LifecyclePhaseNotFoundException

 

 

同じコマンドを通常のコマンドプロンプトで実行すると、問題なく実行できるので、Mavenやpom.xmlに問題がある訳ではないことが分かる。

f:id:kidani_a:20191009025804p:plain

C:\sandbox\mvntest\sample>mvn install -Dskip.yarn
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building sample 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- frontend-maven-plugin:1.8.0:install-node-and-yarn (install node and yarn) @ sample ---
[INFO] Node v10.16.3 is already installed.
[INFO] Yarn 1.17.3 is already installed.
[INFO]
[INFO] --- frontend-maven-plugin:1.8.0:yarn (yarn install) @ sample ---
[INFO] Skipping execution.
[INFO]
[INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ sample ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\sandbox\mvntest\sample\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ sample ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ sample ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\sandbox\mvntest\sample\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ sample ---
[INFO] Nothing to compile - all classes are up to date
[INFO]
[INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ sample ---
[INFO]
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.kdnakt.AppTest
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.057 s - in com.kdnakt.AppTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO]
[INFO] --- maven-jar-plugin:3.0.2:jar (default-jar) @ sample ---
[INFO]
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ sample ---
[INFO] Installing C:\sandbox\mvntest\sample\target\sample-1.0-SNAPSHOT.jar to C:\Users\XXXXXX\.m2\repository\com\kdnakt\sample\1.0-SNAPSHOT\sample-1.0-SNAPSHOT.jar
[INFO] Installing C:\sandbox\mvntest\sample\pom.xml to C:\Users\XXXXXX\.m2\repository\com\kdnakt\sample\1.0-SNAPSHOT\sample-1.0-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.360 s
[INFO] Finished at: 2019-10-XXTXX:XX:XX+09:00
[INFO] Final Memory: 10M/25M
[INFO] ----------------------------------------------------------------------

 

[利用ツールとプロジェクト構成]

前提となる利用ツールのバージョンは以下の通り(やや古いが……)。

C:\sandbox\mvntest>mvn -v
Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-11T01:41:47+09:00)
Maven home: C:\Users\XXXXXX\Documents\apache-maven-3.3.9\bin\..
Java version: 1.8.0_222, vendor: AdoptOpenJDK
Java home: C:\Program Files (x86)\AdoptOpenJDK\jdk-8.0.222.10-hotspot\jre
Default locale: ja_JP, platform encoding: MS932
OS name: "windows 10", version: "10.0", arch: "x86", family: "dos"
C:\sandbox\mvntest>yarn -v
1.17.3

 

プロジェクトの構成は次のようになっている。

Mavenプロジェクトを、mvn archetype:generateコマンドで新規作成した上で、プロジェクトのディレクトリ直下にfrontendディレクトリを作成し、その中でyarn create react-app --language=typescriptコマンドを実行して、package.jsonと関連リソースを生成してある。そして、これらのfrontendリソースをMavenでのビルド時に扱うため、以下のfrontend-maven-pluginを利用している。

github.com

 

pom.xmlは次のようになっている。<!-- My Configuration -->以降がmvn archetype:generateコマンドで生成されたpom.xmlを変更した部分である。

検証用のプロジェクトなので、先述のプラグインを利用してnodeとyarnをインストールし、yarn installコマンドを実行するだけの簡単な構成である。実際のプロジェクトではyarn buildyarn testの実行を定義している部分もあるが、今回の本筋ではないため省略する。

<?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>

<groupId>com.kdnakt</groupId>
<artifactId>sample</artifactId>
<version>1.0-SNAPSHOT</version>

<name>sample</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<!-- My Configuration -->
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<version>1.8.0</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<configuration>
<workingDirectory>frontend</workingDirectory>
<nodeVersion>v10.16.3</nodeVersion>
<yarnVersion>v1.17.3</yarnVersion>
</configuration>
<executions>
<execution>
<id>install node and yarn</id>
<goals>
<goal>install-node-and-yarn</goal>
</goals>
<phase>initialize</phase>
</execution>
<execution>
<id>yarn install</id>
<goals>
<goal>yarn</goal>
</goals>
<phase>initialize</phase>
<configuration>
<arguments>install</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project> 

 

ここでmvn installコマンドを実行すると、次のようにyarn installの処理が実行される。

C:\sandbox\mvntest\sample>mvn install
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building sample 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- frontend-maven-plugin:1.8.0:install-node-and-yarn (install node and yarn) @ sample ---
[INFO] Node v10.16.3 is already installed.
[INFO] Yarn 1.17.3 is already installed.
[INFO]
[INFO] --- frontend-maven-plugin:1.8.0:yarn (yarn install) @ sample ---
[INFO] Running 'yarn install' in C:\sandbox\mvntest\sample\frontend
[INFO] yarn install v1.17.3
[INFO] info No lockfile found.
[INFO] [1/4] Resolving packages...
[INFO] [2/4] Fetching packages...
[INFO] [3/4] Linking dependencies...
[INFO] [4/4] Building fresh packages...
[INFO] success Saved lockfile.
[INFO] Done in 0.14s.
(省略)

 

プロジェクト内のJavaの実装のみ変更があった場合に、これらのyarn関連の処理をスキップしてビルドを実行したい。

frontend-maven-pluginにはそのための機構が用意されており、mvn install -Dskip.yarnという風にシステムプロパティを設定してコマンドを実行すると。yarn関連の処理をスキップすることができる(npmやgulpの場合も同様)。

 

[解決策:PowerShellエスケープ文字列(`)] 

f:id:kidani_a:20191009024904p:plain

PowerShellの画面をよく見ると、そもそもコマンドを入力した時点で「.yarn」の部分の文字色が何故かグレーから白に変わってしまっている。

いつものように表示されたエラーメッセージをそのままGoogle 検索にかけることはせず、何らかのエスケープ処理に問題があると判断し、「PowerShell エスケープ」などと検索するといくつかのサイトにたどり着いた。

https://support.microsoft.com/ja-jp/help/2918008

Escape characters - PowerShell - SS64.com

 

ドットの部分がいけないのか?と思い色々試した結果、PowerShellの場合はmvn install `-Dskip.yarnのように-Dの直前にアクサングラーブ(`)をつけてエスケープ処理を挟むことで、コマンドが正常終了するようになった。

f:id:kidani_a:20191009034142p:plain

PS C:\sandbox\mvntest\sample> mvn install `-Dskip.yarn
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building sample 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- frontend-maven-plugin:1.8.0:install-node-and-yarn (install node and yarn) @ sample ---
[INFO] Node v10.16.3 is already installed.
[INFO] Yarn 1.17.3 is already installed.
[INFO]
[INFO] --- frontend-maven-plugin:1.8.0:yarn (yarn install) @ sample ---
[INFO] Skipping execution.
[INFO]
(省略)

 

[まとめ]

  • frontend-maven-pluginを使うとMavenプロジェクト内でnpmやyarnを利用したビルドが実行できる
  • frontend-maven-pluginのyarn関連の処理をスキップする場合には-Dskip.yarnを設定する
  • PowerShellでコマンドを実行する場合には`-Dskip.yarnのようにエスケープが必要