Java之maven打完jar包之后将jar包放到指定位置汇总

 更新时间:2023年04月11日 08:26:22   作者:前方一片光明  
这篇文章主要介绍了Java之maven打完jar包之后将jar包放到指定位置汇总,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

前言

maven打完jar包之后,默认放置位置是target目录

有时候项目需要,如何将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>是删除命令;

  • ${basedir} 指的是 项目根路径 
  • ${project.build.directory} 指的是 target所在目录
  • ${project.build.finalName} 指的是 jar包前缀名

方式四

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

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • kaptcha验证码组件使用简介解析

    kaptcha验证码组件使用简介解析

    这篇文章主要介绍了kaptcha验证码组件使用简介解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-08-08
  • IDEA提示 add *** to custom tags问题及解决

    IDEA提示 add *** to custom tags问题及解决

    文章介绍了如何在文档注释中添加自定义注解(@xxx),并提供了添加和删除注解的方法,总结了个人经验,希望对大家有所帮助
    2024-12-12
  • 基于@ComponentScan注解的使用详解

    基于@ComponentScan注解的使用详解

    这篇文章主要介绍了@ComponentScan注解的使用详解,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-08-08
  • 三种Spring BeanName生成器,你了解吗

    三种Spring BeanName生成器,你了解吗

    无论我们是通过 XML 文件,还是 Java 代码,亦或是包扫描的方式去注册 Bean,都可以不设置BeanName,而Spring均会为之提供默认的 beanName,本文我们就来看看 Spring 中三种处理不同情况的 beanName生成器吧
    2023-09-09
  • 详谈ThreadLocal-单例模式下高并发线程安全

    详谈ThreadLocal-单例模式下高并发线程安全

    这篇文章主要介绍了ThreadLocal-单例模式下高并发线程安全,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-09-09
  • 关于弱引用WeakReference所引用的对象的回收规则

    关于弱引用WeakReference所引用的对象的回收规则

    这篇文章主要介绍了关于弱引用WeakReference所引用的对象的回收规则,如果一个弱引用实例的成员变量referent引用了一个对象obj,那么就称这个弱引用实例对obj的引用是弱引用,被一个弱引用实例引用的对象,称为弱引用对象,需要的朋友可以参考下
    2023-09-09
  • SpringBoot封装响应数据实现过程详解

    SpringBoot封装响应数据实现过程详解

    这篇文章主要介绍了SpringBoot封装响应数据实现过程,SpringBoot响应数据封装是指在SpringBoot应用程序中,将返回的数据进行封装,以便于前端页面或其他客户端使用,感兴趣想要详细了解可以参考下文
    2023-05-05
  • 解决运行jar包出错:ClassNotFoundException问题

    解决运行jar包出错:ClassNotFoundException问题

    这篇文章主要介绍了解决运行jar包出错:ClassNotFoundException问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-12-12
  • 使用ServletInputStream()输入流读取图片方式

    使用ServletInputStream()输入流读取图片方式

    这篇文章主要介绍了使用ServletInputStream()输入流读取图片方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-10-10
  • springboot 加载 META-INF/spring.factories方式

    springboot 加载 META-INF/spring.factories方式

    这篇文章主要介绍了springboot 加载 META-INF/spring.factories方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-10-10

最新评论