Java之怎么实现maven打完jar包之后将jar包放到指定位置

发布时间:2023-05-06 10:57:05 作者:iii
来源:亿速云 阅读:110

这篇文章主要介绍“Java之怎么实现maven打完jar包之后将jar包放到指定位置”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Java之怎么实现maven打完jar包之后将jar包放到指定位置”文章能帮助大家解决问题。

方式一

通过maven-jar-plugin指定outputDirectory输出路径

可以排除某些配置文件,没有文件夹的话会自动创建!

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

方式二

通过maven-resources-plugin指定outputDirectory输出路径

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

方式三

通过maven-antrun-plugin复制jar包

Maven已经成为Java 工业领域事实上的构建标准,但在某些情况下,如果可以用Ant命令,还是很方便的。

借助 maven-antrun-plugin 插件,可以在Maven执行时,额外执行Ant脚本如下列配置所示:

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

<execution>是可执行命令,可以修改maven的命令执行过程,下面的两个execution是修改了install和clean;

<echo>是打印命令;

<mkdir>是创建文件夹命令;(文件夹里面没有东西时好像不会创建出来)

<copy>是复制命令,其中todir是目标文件夹,overwrite是覆盖旧文件,<fileset dir="xxxx">是源文件,<include>是包含jar包;

<move>是移动文件或者修改名称命令

<delete>是删除命令;

方式四

通过maven-antrun-plugin嵌入build.xml文件

如下列配置所示:

将build.xml放到项目根路径下,使用<ant antfile="${basedir}/build.xml">嵌入build.xml文件即可。

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

方式五

使用distributionManagement设置存放路径

这种方式没有通过插件,而是直接配置distributionManagement

使用deploy命令可以部署到目标文件夹,没有文件夹的话会自动创建!

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

扩展:使用maven-dependency-plugin 插件将依赖包导出到指定文件夹

这种方式是将依赖包输出到指定路径

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

关于“Java之怎么实现maven打完jar包之后将jar包放到指定位置”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注亿速云行业资讯频道,小编每天都会为大家更新不同的知识点。

推荐阅读:
  1. php和java有哪些区别
  2. java和python有什么区别

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java maven jar

上一篇:Java Stream API使代码更出色的操作方法是什么

下一篇:java不同版本在多线程中怎么使用随机数生成器

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》