Your maven repository on GitHub

Developers can upload their library to GitHub, for which it has a special site-maven-plugin plugin . Let's look at an example of its use:

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

Let's see what is written here.

The creation of a temporary local repository is highlighted in blue. Technically it's just a folder, but we need Maven to treat it as a separate repository.

We highlighted in red the launch of the maven-deploy-plugin plugin , where we indicated that the compiled library should be placed in this temporary repository.

And finally, the site-maven-plugin plugin is highlighted in green , which should take all the files from the repository and commit them to GitHub. Some explanation is needed here. All parameters are divided into two groups: what to fill and where to fill.

What we fill in:
  • outputDirectory - directory where to get files for commit
  • includes - sets the mask of files to commit
Where do we upload:
  • repositoryOwner - the name of the repository owner on GitHub
  • repositoryName - repository name
  • branch - sets the repository branch on GitHub to which to commit
  • message - the message that will be added when committing

You also need to specify the login and password for your repository in Maven setting.xml :

<settings>
  <servers>
    <server>
  	<id>github</id>
      <username>[username]</username>
      <password>[password]</password>
    </server>
  </servers>
</settings>

To connect (use) a library from a GitHub repository to another project, you need to specify this repository in your 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>

After that, Maven will understand where to get the library from.

  • [name-project] is the name of the project, in our case SuperLibrary
  • [username] is the login on GitHub, in the example it is codegym-user

Packing the assembly into a Docker image

We live in a new time, when projects as a result of the assembly can be placed in the Maven repository, or maybe in docker storage.

To make Maven and Docker friends, we need the docker-maven-plugin plugin . Nothing complicated:

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

Highlighted in blue is the point where we added the goal bulid to the package phase of the build. It can be called with the mvn docker:build command .

The dockerDirectory tag specifies the folder where the Dockerfile is located. And the name of the image is set using the imageName tag .

If the project is packaged in a jar file, then the docker file will look something like this:

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

If you are packaging a web application, then you may need to add Tomcat:

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