W
W
whoami?root root_toor2018-07-03 10:12:49
Java
whoami?root root_toor, 2018-07-03 10:12:49

How to write a Maven plugin that automates the generation of an update archive at the time the project is built?

Hello!
I have a Java project and I need to make a maven plugin to prepare a zip archive with program updates. The archive has a simple structure: libraries in the lib directory, updates in the upd directory, and a text file with a build number.
The task is simple - copy the files to the appropriate directories and compress the output root folder into a *.zip archive at the build stage.
I found several plugins that can copy zip files as well:
The Mojo Executer - for executing homemade or other plugins.
Antrun/Maven-build plugin-to copy directories and zip folders,
Maven-resources-plugin-to copy specified files.
In addition, I can write my own @MOJO - a class that will manually copy directories using Apach FileUtils, for example, and do the compression using Java code in my @MOJO class).
Please advise which way is correct? Write a Mojo class with the described actions or use ready-made plugins?
How can I set the directories in the assembly plugin that I need to copy and create an archive?
How to write build-number to a file?
Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DS1977, 2018-08-03
@DS1977

I don't know if it's correct or not, but I do this:

1. I collect all the libraries in one jar:
<plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-shade-plugin</artifactId>
                        <version>2.3</version>
                        <configuration>
                            <createDependencyReducedPom>true</createDependencyReducedPom>
                            <filters>
                                <filter>
                                    <artifact>*:*</artifact>
                                    <excludes>
                                        <exclude>META-INF/*.SF</exclude>
                                        <exclude>META-INF/*.DSA</exclude>
                                        <exclude>META-INF/*.RSA</exclude>
                                    </excludes>
                                </filter>
                            </filters>
                        </configuration>
                        <executions>
                            <execution>
                                <phase>package</phase>
                                <goals>
                                    <goal>shade</goal>
                                </goals>
                                <configuration>
                                    <transformers>
                                        <transformer
                                                implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                                        <transformer
                                                implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                            <mainClass>com.example.Main</mainClass>
                                        </transformer>
                                    </transformers>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>

2. I archive
<plugin>
                        <artifactId>maven-assembly-plugin</artifactId>
                        <version>2.5.3</version>
                        <configuration>
                            <descriptor>src/main/assembly/dep.xml</descriptor>
                        </configuration>
                        <executions>
                            <execution>
                                <id>create-archive</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>single</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>

dep.xml
<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
    <id>src</id>
    <formats>
        <format>tar.gz</format>
    </formats>
    <includeBaseDirectory>false</includeBaseDirectory>
    <fileSets>
        <fileSet>
            <directory>${project.basedir}</directory>
            <includes>
                <include>README*</include>
                <include>LICENSE*</include>
                <include>NOTICE*</include>
            </includes>
            <useDefaultExcludes>true</useDefaultExcludes>
        </fileSet>
        <fileSet>
            <directory>${project.basedir}</directory>
            <outputDirectory>/</outputDirectory>
            <includes>
                <include>config.properties</include>
                <include>start.sh</include>
                <include>start.bat</include>
            </includes>
        </fileSet>
    </fileSets>
    <files>
        <file>
            <outputDirectory>/</outputDirectory>
            <source>${project.build.directory}/myproject-${project.version}.jar</source>
            <destName>myproject.jar</destName>
        </file>
    </files>
</assembly>

3. I configure deployment:
<plugin>
                        <artifactId>maven-deploy-plugin</artifactId>
                        <version>2.8.2</version>
                        <executions>
                            <execution>
                                <id>deploy-binaries</id>
                                <goals>
                                    <goal>deploy-file</goal>
                                </goals>
                                <configuration>
                                    <file>target/myproject-${project.version}-src.tar.gz</file>
                                    <repositoryId>repo</repositoryId>
                                    <artifactId>${project.artifactId}</artifactId>
                                    <groupId>${project.groupId}</groupId>
                                    <version>${project.version}</version>
                                    <packaging>tgz</packaging>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>

You can write build-number to a file with ant-plugin:
Watch here

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question