1. Свій 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>javarush-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. Тут потрібні деякі пояснення. Усі параметри поділяються на дві групи: що заливаємо та куди заливаємо.

Що заливаємо:
  • outputDirectory – директорія, звідки беруться файли для коміту
  • includes – вказує маску файлів для коміту
Куди заливаємо:
  • repositoryOwner – ім'я власника репозиторію на GitHub
  • repositoryName – ім'я репозиторію
  • branch – вказує гілку репозиторію на GitHub, в яку комітити
  • message - повідомлення, яке додається при коміті

Також потрібно вказати логін та пароль до свого репозиторію в Maven setting.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, у прикладі це javarush-user

2. Запаковуємо складання проєкту в Docker образ

Ми живемо в новий час, коли проєкти в результаті складання можуть розміщуватися в Maven-репозиторій, а можуть і в docker storage.

Щоб подружити 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>javarush/${project.artifactId}</imageName>
    	</configuration>
    	<executions>
      	  <execution>
            <phase>package</phase>
        	<goals>
          	<goal>build</goal>
        	</goals>
      	  </execution>
    	</executions>
  	  </plugin>
    </plugins>
  </build>

Синім виділено момент, де ми додали goal build у package-фазу складання. Його можна викликати за допомогою команди mvn docker:build.

Тег dockerDirectory вказує папку, де знаходиться Dockerfile. А ім'я образу встановлюється за допомогою тега imageName.

Якщо проєкт упаковано в jar-файл, docker-файл виглядатиме приблизно так:

FROM java:11
EXPOSE 8080
ADD /target/demo.jar demo.jar
ENTRYPOINT ["java","-jar","demo.jar"]

В разі якщо ти запаковуєш вебзастосунок, може знадобитися додати Tomcat:

FROM tomcat8
ADD sample.war ${CATALINA_HOME}/webapps/ROOT.war
CMD ${CATALINA_HOME}/bin/catalina.sh run