1. 概要

このチュートリアルでは、 site-mavenプラグインを使用して、ソースを使用してGitHubでMavenリポジトリをホストする方法を理解します。 これは、Nexusのようなリポジトリを使用するための手頃な代替手段です。

2. 前提条件

まだ持っていない場合は、GitHubでMavenプロジェクトのリポジトリを作成する必要があります。 この記事では、1つのリポジトリ「 host-maven-repo-example 」と、ブランチ「main」を使用しています。 これはGitHubの空のリポジトリです:

3. Mavenプロジェクト

簡単なMavenプロジェクトを作成しましょう。 このプロジェクトで生成されたアーティファクトをGitHubにプッシュします。

プロジェクトの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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.baeldung.maven.plugin</groupId>
    <artifactId>host-maven-repo-example</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
<project>

まず、プロジェクトローカルリポジトリをローカルに作成する必要があります。 Mavenアーティファクトは、GitHubにプッシュする前に、プロジェクトビルドディレクトリのこの場所にデプロイされます。

ローカルリポジトリ定義をpom.xmlに追加します。

<distributionManagement> 
    <repository>
        <id>internal.repo</id> 
        <name>Temporary Staging Repository</name> 
        <url>file://${project.build.directory}/mvn-artifact</url> 
    </repository> 
</distributionManagement>

それでは、maven-deploy-plugin構成pom.xmlに追加しましょう。 このプラグインを使用して、アーティファクトをディレクトリ $ {project.build.directory} /mvn-artifactのローカルリポジトリに追加します。

<plugin>
    <artifactId>maven-deploy-plugin</artifactId>
    <version>2.8.2</version>
    <configuration>
        <altDeploymentRepository>
            internal.repo::default::file://${project.build.directory}/mvn-artifact
        </altDeploymentRepository>
    </configuration>
</plugin>

また、 Mavenアーティファクトを含むソースファイルをGitHubにプッシュする場合は、ソースプラグインも含める必要があります。

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-source-plugin</artifactId>
    <version>3.1.0</version>
    <executions>
        <execution>
            <id>attach-sources</id>
                <goals>
                    <goal>jar</goal>
                </goals>
        </execution>
    </executions>
</plugin>

上記の構成とプラグインがpom.xmlに追加されると、ビルドはMavenアーティファクトをディレクトリ target /mvn-artifactにローカルにデプロイします。

ここで、次のステップは、これらのアーティファクトをローカルディレクトリからGitHubにデプロイすることです。

4. GitHub認証を構成する

アーティファクトをGitHubにデプロイする前に、〜/。 m2 /settings.xmlで認証情報を構成します。 これは、site-maven-pluginがアーティファクトをGitHubにプッシュできるようにするためです。

認証方法に応じて、2つの構成のいずれかをsettings.xmlに追加します。 次に、これらのオプションを確認しましょう。

4.1. GitHubのユーザー名とパスワードの使用

GitHubのユーザー名とパスワードを使用するには、settings.xmlで構成します。

<settings>
    <servers>
        <server>
            <id>github</id>
            <username>your Github username</username>
            <password>your Github password</password>
        </server>
    </servers>
</settings>

4.2. パーソナルアクセストークンの使用

GitHub APIまたはコマンドラインを使用するときに推奨される認証方法は、パーソナルアクセストークン(PAT)を使用することです

<settings>
    <servers> 
        <server>
            <id>github</id>
            <password>YOUR GitHub OAUTH-TOKEN</password>
        </server>
    </servers>
</settings>

5. site-maven-pluginを使用してアーティファクトをGitHubにプッシュします

そして最後のステップは、ローカルステージングリポジトリをプッシュするようにsite-mavenプラグインを構成することです。 このステージングリポジトリは、targetディレクトリにあります。

<plugin>
    <groupId>com.github.github</groupId>
    <artifactId>site-maven-plugin</artifactId>
    <version>0.12</version>
    <configuration>
        <message>Maven artifacts for ${project.version}</message>
        <noJekyll>true</noJekyll>
        <outputDirectory>${project.build.directory}</outputDirectory>
        <branch>refs/heads/${branch-name}</branch>
        <includes>
            <include>**/*</include>
        </includes>
        <merge>true</merge>
        <repositoryName>${repository-name}</repositoryName>
        <repositoryOwner>${repository-owner}</repositoryOwner>
        <server>github</server>
    </configuration>
    <executions>
        <execution>
            <goals>
                <goal>site</goal>
            </goals>
            <phase>deploy</phase>
        </execution>
    </executions>
</plugin>

例として、このチュートリアルでは、リポジトリ eugenp /host-maven-repo-exampleがあるとします。 その場合、repositoryNameタグの値はhost-maven-repo-exampleになり、repositoryOwnerタグの値はeugenpになります。

次に、 mvn deployコマンドを実行して、アーティファクトをGitHubにアップロードします。 main ブランチは、存在しない場合は自動的に作成されます。 ビルドが成功したら、ブラウザのメインブランチの下にあるGitHubのリポジトリを確認します。 すべてのバイナリがリポジトリに表示されます。

この場合、次のようになります。

6. 結論

最後に、 site-maven-plugin を使用して、GitHubでMavenアーティファクトをホストする方法を確認しました。

いつものように、これらの例のコードはGitHubから入手できます。