@kyanny's blog

My thoughts, my life. Views/opinions are my own.

Maven: artifactId に大文字は使えない

Maven – Guide to Naming Conventions

artifactId is the name of the jar without version. If you created it, then you can choose whatever name you want with lowercase letters and no strange symbols. If it's a third party jar, you have to take the name of the jar as it's distributed. eg. maven, commons-math

逆にいうと upper case letters は使えない。

pom.xml で大文字を含む artifactId が指定されてる場合、jar ファイルをアップロードする maven リポジトリによっては 422 Unprocessable Entity エラーになることがある。


GitHub Packages の Apache Maven レジストリで実験する。なお作業は Ubuntu 20.04.3 LTS 上で行った。

Maven – Maven in 5 Minutes の手順に従って maven パッケージを作る。ただし、わざと artifactId に大文字アルファベットを含める。

mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=myApp -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false

GitHub Packages の Apache Maven レジストリ の例の通り ~/.m2/settings.xml を作る。

mkdir -p ~/.m2/
cat <<EOM> ~/.m2/settings.xml
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
                      http://maven.apache.org/xsd/settings-1.0.0.xsd">

  <activeProfiles>
    <activeProfile>github</activeProfile>
  </activeProfiles>

  <profiles>
    <profile>
      <id>github</id>
      <repositories>
        <repository>
          <id>central</id>
          <url>https://repo1.maven.org/maven2</url>
        </repository>
        <repository>
          <id>github</id>
          <url>https://maven.pkg.github.com/OWNER/*</url>
          <snapshots>
            <enabled>true</enabled>
          </snapshots>
        </repository>
      </repositories>
    </profile>
  </profiles>

  <servers>
    <server>
      <id>github</id>
      <username>USERNAME</username>
      <password>TOKEN</password>
    </server>
  </servers>
</settings>
EOM
sed -i 's/OWNER/kyanny/' ~/.m2/settings.xml
sed -i 's/USERNAME/kyanny/' ~/.m2/settings.xml
sed -i "s/TOKEN/$GITHUB_TOKEN/" ~/.m2/settings.xml

あらかじめコマンドを実行するシェル上で export GITHUB_TOKEN=XXXX という感じで GitHub の Personal Access Token をセットしておくこと。

pom.xml に < distributionManagement> タグを追記するのがならわしのようだが、-D コマンドラインオプションで指定することも可能。どのようなオプションを指定すればよいかは Apache Maven Deploy Plugin – deploy:deploy に書いてある。

<distributionManagement> のコマンドライン指定用プロパティ名は altDeploymentRepository で、-D オプションで指定する際のフォーマットは id::layout::url とのこと。

ここで id は settings.xml の <server> タグの中に書いたもの・・だと思う。

id The id can be used to pick up the correct credentials from the settings.xml url The location of the repository

とあるので。ここでは https://github.com/kyanny/hello リポジトリに対してパッケージのアップロードを試みるので、url はこのリポジトリの URL になる。

ドキュメントには、

Note: Since 3.0.0 the layout part has been removed.

とも書いてあり、id::url という書式に変更になったのかと思ったが、-DaltDeploymentRepository=github::https://github.com/kyanny/hello と指定したらエラーになった。

Mavenめも - するめとめがね によると、

レイアウトは3.xと2.xの場合はdefault、1.xはlegacyを指定すればいいっぽい?

とのことで、-DaltDeploymentRepository=github::default::https://github.com/kyanny/hello としたらうまくいった。

-l はログをファイルに書き込むオプション。標準出力に出るログはデフォルトで色がついてて見やすいが、-l オプションで出力すると色がつかないので、ファイルにエスケープシーケンスが混じらない。

mvn deploy -DaltDeploymentRepository=github::default::https://github.com/kyanny/hello -l deploy.log

しかしこれは 422 Unprocessable Entity エラーで失敗する。が、それは再現実験が成功したということなので想定内。

deploy.log · GitHub

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.8.2:deploy (default-deploy) on project myApp: Failed to deploy artifacts: Could not transfer artifact com.mycompany.app:myApp:jar:1.0-20210903.175557-1 from/to github (https://github.com/kyanny/hello): Transfer failed for https://github.com/kyanny/hello/com/mycompany/app/myApp/1.0-SNAPSHOT/myApp-1.0-20210903.175557-1.jar 422 Unprocessable Entity -> [Help 1]

Maven: mvn deploy の詳細なログを出力する - @kyanny's blog に書いたとおり、-Dorg.slf4j.simpleLogger.log.org.apache.maven.wagon.providers.http.httpclient.wire=trace オプションをつけると 422 Unprocessable Entity エラーの原因がわかる。残念ながら -e オプションや -X オプションでは、ログ出力は増えるものの、このケースの解決のためには足りない。

mvn deploy -DaltDeploymentRepository=github::default::https://github.com/kyanny/hello -Dorg.slf4j.simpleLogger.log.org.apache.maven.wagon.providers.http.httpclient.wire=trace -l deploy.log

...と、ここで本当は 422 Unprocessable Entity のレスポンスボディに「artifactId に upper case letter は使えない」というドンピシャのメッセージが書いてある、という想定だったのだが、そうならなかった。残念。