GitHub 上の Maven リポジトリ
開発者はライブラリを GitHub にアップロードできます。GitHub には特別なsite-maven-plugin プラグインがあります。その使用例を見てみましょう。
<project>
<properties>
<github.global.server>github</github.global.server>
<github.maven-plugin>0.9</github.maven-plugin>
</properties>
<distributionManagement>
<repository>
<id>internal.repo</id>
<name>Temporary Staging Repository</name>
<url>file://${project.build.directory}/mvn-repo</url>
</repository>
</distributionManagement>
<build>
<plugins>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.1</version>
<configuration>
<altDeploymentRepository>
internal.repo::default::file://${project.build.directory}/mvn-repo
</altDeploymentRepository>
</configuration>
</plugin>
<plugin>
<groupId>com.github.github</groupId>
<artifactId>site-maven-plugin</artifactId>
<version>${github.maven-plugin}</version>
<configuration>
<message>Maven artifacts for ${project.version}</message>
<noJekyll>true</noJekyll>
<outputDirectory>${project.build.directory}/mvn-repo</outputDirectory>
<branch>refs/heads/mvn-repo</branch>
<includes>**/*</includes>
<repositoryName>SuperLibrary</repositoryName>
<repositoryOwner>codegymu-student</repositoryOwner>
</configuration>
<executions>
<execution>
<goals>
<goal>site</goal>
</goals>
<phase>deploy</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
ここに何が書かれているか見てみましょう。
一時ローカル リポジトリの作成は青色で強調表示されます。技術的にはこれは単なるフォルダーですが、Maven がそれを別のリポジトリとして扱う必要があります。
maven-deploy-plugin プラグインの起動を赤色で強調表示し、コンパイルされたライブラリをこの一時リポジトリに配置する必要があることを示しました。
最後に、site-maven-plugin プラグインが緑色で強調表示されます。これにより、リポジトリからすべてのファイルが取得され、GitHub にコミットされます。ここで少し説明が必要です。すべてのパラメータは、何を入力するか、どこに入力するかという 2 つのグループに分けられます。
記入する内容:- OutputDirectory - コミット用のファイルを取得するディレクトリ
- include - コミットするファイルのマスクを設定します
- repositoryOwner - GitHub 上のリポジトリ所有者の名前
- repositoryName - リポジトリ名
- ブランチ- コミット先の GitHub 上のリポジトリ ブランチを設定します
- message - コミット時に追加されるメッセージ
Maven settings.xmlでリポジトリのログインとパスワードを指定する必要もあります。
<settings>
<servers>
<server>
<id>github</id>
<username>[username]</username>
<password>[password]</password>
</server>
</servers>
</settings>
GitHub リポジトリのライブラリを別のプロジェクトに接続 (使用) するには、このリポジトリをpom.xmlで指定する必要があります。
<repositories>
<repository>
<id>[name-project]-mvn-repo</id>
<url>https://raw.github.com/[username]/[name-project]/mvn-repo/</url>
<snapshots>
<enabled>true</enabled>
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
</repositories>
その後、Maven はライブラリをどこから取得すればよいかを理解します。
- [name-project]はプロジェクトの名前です。この場合は SuperLibrary です。
- [username]は GitHub のログイン名で、この例では codegym-user です。
アセンブリを Docker イメージにパッケージ化する
私たちは、アセンブリの結果としてのプロジェクトを Maven リポジトリ、または Docker ストレージに配置できる新しい時代に生きています。
Maven と Docker を友達にするには、 docker-maven-plugin プラグインが必要です。何も複雑なことはありません:
<build>
<plugins>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.10</version>
<configuration>
<dockerDirectory>${project.basedir}</dockerDirectory>
<imageName>codegym/${project.artifactId}</imageName>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
青で強調表示されているのは、ビルドのパッケージ段階にゴール bulid を追加したポイントです。これは、 mvn docker:buildコマンドで呼び出すことができます。
dockerDirectoryタグは、Dockerfile が配置されているフォルダーを指定します。また、画像の名前はimageNameタグを使用して設定されます。
プロジェクトが jar ファイルにパッケージ化されている場合、docker ファイルは次のようになります。
FROM java:11
EXPOSE 8080
ADD /target/demo.jar demo.jar
ENTRYPOINT ["java","-jar","demo.jar"]
Web アプリケーションをパッケージ化している場合は、Tomcat を追加する必要がある場合があります。
FROM tomcat8
ADD sample.war ${CATALINA_HOME}/webapps/ROOT.war
CMD ${CATALINA_HOME}/bin/catalina.sh run