Home  >  Article  >  Java  >  How to implement in Java how to place the jar package in the specified location after maven completes the jar package

How to implement in Java how to place the jar package in the specified location after maven completes the jar package

王林
王林forward
2023-05-13 11:46:051195browse

Method 1

Specify the outputDirectory output path through maven-jar-plugin

You can exclude certain configuration files. If there is no folder, it will be automatically created!

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <configuration>
        <!-- 指定打包的jar包输出路径 -->
        <outputDirectory>D:\test</outputDirectory>
        <!--不打入jar包的文件类型或者路径 -->
        <excludes>
            <exclude>**/*.properties</exclude>
            <exclude>**/*.xml</exclude>
            <exclude>**/*.yml</exclude>
            <exclude>static/**</exclude>
            <exclude>templates/**</exclude>
        </excludes>
    </configuration>
</plugin>

Method 2

Specify the outputDirectory output path through maven-resources-plugin

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <executions>
        <execution>
            <id>copy-resources</id>
            <phase>package</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <encoding>UTF-8</encoding>
                <!--打成jar包后复制到的路径 -->
                <outputDirectory>
                    D:\test1
                </outputDirectory>
                <resources>
                    <resource>
                        <!--项目中的路径 -->
                        <directory>src/main/resources/</directory>
                    </resource>
                </resources>
            </configuration>
        </execution>
        <!--可配置多个提取复制路径只需要 “<id>”名字不一样即可 -->
        <execution>
            <id>copy-bulid</id>
            <phase>package</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <encoding>UTF-8</encoding>
                <outputDirectory>
                    D:\test2
                </outputDirectory>
                <resources>
                    <resource>
                        <directory>target</directory>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>

Method 3

Copy the jar package through maven-antrun-plugin

Maven has become the de facto build standard in the Java industry, but in some cases, it is still very convenient if you can use the Ant command.

With the maven-antrun-plugin plug-in, you can additionally execute Ant scripts when Maven is executed, as shown in the following configuration:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.8</version>
    <executions>
        <execution>
            <id>install</id>
            <phase>install</phase>
            <configuration>
                <target>
                    <echo message="*******************install*******************" />
                    <mkdir dir="${basedir}/target/classes" />
                    <copy todir="../target/commons" overwrite="true">
                        <fileset dir="${project.build.directory}"
                            erroronmissingdir="false">
                            <include name="*.jar" />
                        </fileset>
                    </copy>
                    <move file="${project.build.directory}/xxxxxxx.jar"
                        tofile="${project.build.directory}/xxx.jar" />
                </target>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
        <execution>
            <id>clean</id>
            <phase>clean</phase>
            <configuration>
                <target>
                    <echo message="*******************clean*******************" />
                    <delete dir="target" />
                    <mkdir dir="${basedir}/target/classes" />
                </target>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>

c94ac33d58052ea4ba2b719493f112d4 is an executable command, which can modify maven's During the command execution process, the following two executions modify install and clean;

8a8567de266ae3e91153035db7759a7c is a print command;

b4d0f8a693a26aba7702f1d3a4b09dcb is a folder creation command; (inside the folder It doesn’t seem to be created when there is nothing)

797d7b8e63f3fc0bd7a5dd27123b70d6 is a copy command, where todir is the target folder, overwrite is to overwrite the old file, a1aaab7cce83a48dcd054c43d472d62a is the source file, dcf91641426a34cf32ecc36140f28baf is a jar package included;

cf33f54f92efff0f86f9ed9ee073eee9 is a command to move files or change names

5cc62b85a20462d19109e58cc4ad0bf9 is a delete command;

  • ${basedir} refers to the project root path

  • ${project.build.directory} refers to the directory where the target is located

  • ${project.build.finalName} refers to the jar package prefix name

Method 4

Embed the build.xml file through maven-antrun-plugin

As shown in the following configuration:

Place build.xml in the project root path, use 051b9ea6ed734e760f40cebe431f3387 Just embed the build.xml file.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>1.8</version>
    <executions>
        <execution>
            <id>install</id>
            <phase>install</phase>
            <configuration>
                <target>
                    <property name="compile_classpath"
                        refid="maven.compile.classpath" />
                    <property name="runtime_classpath"
                        refid="maven.runtime.classpath" />
                    <property name="test_classpath"
                        refid="maven.test.classpath" />
                    <property name="plugin_classpath"
                        refid="maven.plugin.classpath" />
 
                    <ant antfile="${basedir}/build.xml">
                        <target name="test" />
                    </ant>
                </target>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Method 5

Use distributionManagement to set the storage path

This method does not use a plug-in, but directly configures distributionManagement

Use the deploy command to deploy to the target folder. If there is no folder, it will be automatically created!

<distributionManagement>
    <repository>
        <id>localRepository</id>
        <url>file:D:/testRepository</url>
    </repository>
</distributionManagement>

Extension: Use the maven-dependency-plugin plug-in to export dependent packages to the specified folder

This method is to output dependent packages to the specified path

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <phase>package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <!-- 指定输出路径 -->
                <outputDirectory>D:\test2</outputDirectory>
                <excludeTransitive>false</excludeTransitive>
                <stripVersion>false</stripVersion>
                <includeScope>runtime</includeScope>
            </configuration>
        </execution>
    </executions>
</plugin>

The above is the detailed content of How to implement in Java how to place the jar package in the specified location after maven completes the jar package. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete