Home > Java > javaTutorial > body text

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

王林
Release: 2023-05-13 11:46:05
forward
1229 people have browsed it

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>
Copy after login

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>
Copy after login

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>
Copy after login

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

is a print command;

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

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

is a command to move files or change names

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 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>
Copy after login

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>
Copy after login

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>
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template