ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

maven插件的使用

2021-09-11 14:33:21  阅读:307  来源: 互联网

标签:插件 plugin jar maven 使用 copy unpack


maven插件的使用

文章目录

1 前言

最近在做某个需求的时候,由于需要将某个功能打成一个jar包,以便其他模块也能使用,于是我就在公司项目下面新建了两个子模块,一个放后台服务,一个放前端的页面(不要问我为什么不用vue啊),然后不可避免的就碰到很多问题(主要是对maven打包不熟悉,我的两个jar包引入项目后一直不生效)。所以趁着周六休息,来学习一下maven插件的使用。

先来重点看如下两个插件,其他插件以后用到了再记录

maven-dependency-plugin

maven-assembly-plugin

2 maven-dependency-plugin

这个插件可以做什么事情呢?由goal标签指定

copy:将指定坐标对应的jar包拷贝到指定目录

unpack:将指定坐标对应的jar包解压到指定目录

2.1 copy

下述配置在打包阶段会将junit包拷贝到项目的target/alternateLocation目录下

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>3.2.0</version>
            <executions>
                <execution>
                    <id>copy</id>
                    <phase>package</phase>
                    <goals>
                        <goal>copy</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <artifactItems>
                    <artifactItem>
                        <groupId>junit</groupId>
                        <artifactId>junit</artifactId>
                        <version>3.8.1</version>
                        <type>jar</type>
                        <overWrite>false</overWrite>
                        <outputDirectory>${project.build.directory}/alternateLocation</outputDirectory>
                    </artifactItem>
                </artifactItems>
            </configuration>
        </plugin>

2.2 copy-dependencies

copy命令的区别是,copy是拷贝指定坐标的jar包,而copy-dependencies则是拷贝项目依赖的所有jar包,具体用法如下

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>3.2.0</version>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <phase>package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.build.directory}/alternateLocation</outputDirectory>
                <overWriteReleases>false</overWriteReleases>
                <overWriteSnapshots>false</overWriteSnapshots>
                <overWriteIfNewer>true</overWriteIfNewer>
            </configuration>
        </execution>
    </executions>
</plugin>

2.3 unpack

unpack命令可以将指定的jar包解压到指定的目录,需要注意的是,如果指定的触发阶段为compile,那么解压之后的内容会一起打包到当前项目中,而如果是package则不会,它只会打包自己项目内容

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>3.2.0</version>
    <executions>
        <execution>
            <!-- 为该操作定义一个名字 -->
            <id>unpack</id>
            <!-- 该操作在生命周期的那个阶段触发 -->
            <phase>compile</phase>
            <goals>
                <!-- 插件的具体操作,这里只介绍4种-->
                <goal>unpack</goal>
            </goals>
            <!-- 操作的配置 -->
            <configuration>
                <!-- 配置 -->
                <artifactItems>
                    <!-- 具体配置,此处一般是针对某个坐标的具体配置 -->
                    <artifactItem>
                        <!-- 指定坐标 -->
                        <groupId>org.example.it</groupId>
                        <artifactId>po.webresource</artifactId>
                        <version>1.0-SNAPSHOT</version>
                        <type>jar</type>
                        <overWrite>true</overWrite>
                        <!-- 输出路径 -->
                        <outputDirectory>${project.build.directory}/classes</outputDirectory>
                        <!-- 坐标对应的jar包的名称 -->
                        <destFileName>po.webresource-1.0-SNAPSHOT.jar</destFileName>
                        <!-- 操作的内容 -->
                        <includes>**/*.html,**/*.js</includes>
                        <!-- 排除的内容 -->
                        <excludes>**/*test.html</excludes>
                    </artifactItem>
                </artifactItems>
            </configuration>
        </execution>
    </executions>
</plugin>

2.4 unpack-dependencies

unpack命令的区别是,unpack是解压指定坐标的jar包,而unpack-dependencies则是解压项目依赖的所有jar包,具体用法如下

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>3.2.0</version>
    <executions>
        <execution>
            <id>unpack-dependencies</id>
            <phase>package</phase>
            <goals>
                <goal>unpack-dependencies</goal>
            </goals>
            <configuration>
                <includes>**/*.class</includes>
                <excludes>**/*.properties</excludes>
                <outputDirectory>${project.build.directory}/alternateLocation</outputDirectory>
                <overWriteReleases>false</overWriteReleases>
                <overWriteSnapshots>true</overWriteSnapshots>
            </configuration>
        </execution>
    </executions>
</plugin>

3 maven-assembly-plugin

有时候我们需要将项目依赖抽出来单独打包,或者将项目里面的html、js等静态资源抽取出来打包,就需要用到这个插件了。插件的使用如下

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-assembly-plugin</artifactId>
    <version>3.1.0</version>
    <executions>
        <execution>
            <!-- 操作名,自己指定 -->
            <id>assembly</id>
            <!-- 操作触发的阶段 -->
            <phase>package</phase>
            <goals>
                <!-- 只执行一次-->
                <goal>single</goal>
            </goals>
            <configuration>
                <!-- 自己指定的名字,和assemblies.xml配置文件中的id标签对应 -->
                <finalName>html</finalName>
                <!-- 配置文件的位置和名字(与pom.xml)的相对路径-->
                <descriptors>assemblies.xml</descriptors>
            </configuration>
        </execution>
    </executions>
</plugin>

除了需要在pom中配置上述内容外,还需要引入一个配置文件assemblies.xml(名字由自己指定)

<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.1.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.1.0 http://maven.apache.org/xsd/assembly-2.1.0.xsd">
    <id>html</id>
    <formats>
        <!-- 生成一个zip文件 -->
        <format>zip</format>
    </formats>
    <fileSets>
        <fileSet>
            <!-- 从编译后的文件中拷贝 -->
            <directory>${project.build.directory}/classes</directory>
            <!-- 拷贝到的文件存放的路径 -->
            <outputDirectory></outputDirectory>
            <useDefaultExcludes>true</useDefaultExcludes>
            <excludes>
                <!-- 哪些文件不拷贝 -->
                <exclude>**/*.log</exclude>
                <exclude>**/${project.build.directory}/**</exclude>
            </excludes>
        </fileSet>
    </fileSets>
</assembly>

上述操作都只涉及这两个插件的简单使用,后面有用到再补充

标签:插件,plugin,jar,maven,使用,copy,unpack
来源: https://blog.csdn.net/qq_33012981/article/details/120237241

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有