精华内容
下载资源
问答
  • maven 自定义插件开发及使用

    千次阅读 2019-05-18 06:55:22
    本文采用maven自定义插件实现了解决html引入css、js缓存的问题。问题解决思路为扫描路径下的html文件,并将html文件中引入的css、js路径后缀加入版本号的方案。如将...

    mave自定义插件

    介绍

    本文采用maven自定义插件实现了解决html引入css、js缓存的问题。问题解决思路为扫描路径下的html文件,并将html文件中引入的css、js路径后缀加入版本号的方案。如将https://code.jquery.com/jquery-3.2.1.slim.min.js变更为https://code.jquery.com/jquery-3.2.1.slim.min.js?v=1558055492531

    要实现自定义插件的开发,基本步骤为:

    • pom文件中加入配置 <packaging>maven-plugin</packaging>,并引入依赖maven-plugin-apimaven-plugin-annotations
    • 新建类继承extends AbstractMojo,并在类头加入注解 @Mojo
    • 如需定义参数,类中参数变量上加入@Parameter注解
    • 开发完成后,使用 mvn install命令将自定义插件加载到本地仓库。

    demo部分源码

    pom文件配置

     

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.yanmei.study</groupId>
        <artifactId>yanmei-maven-plugin</artifactId>
        <version>1.0-SNAPSHOT</version>
        <!--自定义插件开发需加入配置-->
        <packaging>maven-plugin</packaging>
    
        <dependencies>
           <!--自定义插件开发需引入依赖 -->
            <dependency>
                <groupId>org.apache.maven</groupId>
                <artifactId>maven-plugin-api</artifactId>
                <version>3.0</version>
            </dependency>
            <!--自定义插件开发需引入依赖 -->
            <dependency>
                <groupId>org.apache.maven.plugin-tools</groupId>
                <artifactId>maven-plugin-annotations</artifactId>
                <version>3.3</version>
                <scope>provided</scope>
            </dependency>
            <dependency> <!-- 采用jsoup包来处理html页面-->
                <groupId>org.jsoup</groupId>
                <artifactId>jsoup</artifactId>
                <version>1.11.3</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-test</artifactId>
                <version>1.5.7.RELEASE</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-test</artifactId>
                <version>4.3.11.RELEASE</version>
                <scope>test</scope>
            </dependency>
        </dependencies>
    </project>

    新建工程,主类代码为

    package com.yanmei.study.maven.plugin;
    
    import org.apache.maven.plugin.AbstractMojo;
    import org.apache.maven.plugin.MojoExecutionException;
    import org.apache.maven.plugin.MojoFailureException;
    import org.apache.maven.plugins.annotations.LifecyclePhase;
    import org.apache.maven.plugins.annotations.Mojo;
    import org.apache.maven.plugins.annotations.Parameter;
    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    import org.jsoup.nodes.Element;
    import org.jsoup.select.Elements;
    
    import java.io.*;
    
    @Mojo(name="yanmeiplugin", defaultPhase = LifecyclePhase.PACKAGE)
    public class TestDemo extends AbstractMojo{
        @Parameter
        private String dirPath;
        @Parameter
        private Boolean skip=false;
    
        public void execute() throws MojoExecutionException, MojoFailureException {
            if(skip == true){
                System.out.println("skip is true");
                return;
            }
            if(dirPath == null || dirPath.equals("")){
                System.out.println("the param can't be empty");
                return;
            }
            System.out.println("the param is "+dirPath);
            File file = new File(dirPath);
            findFile(file);
        }
       // 查找文件
        private void findFile(File files){
            if(!files.exists()) {
                System.out.println("the file or the dir is not exist!");
                return;
            }
    
            File[] childFiles = files.listFiles();
            if(childFiles == null || childFiles.length<=0){
                System.out.println("the file or the dir is not exist");
                return;
            }
            for(File file: childFiles){
                if(file.isDirectory()){
                    findFile(file);
                }else {
                    // 处理文件
                    processFile(file);
                }
    
            }
    
        }
        // 对找到的html文件进行处理
        private void processFile(File file) {
            String name = file.getName();
            if (name.endsWith(".html")) {
                Document document = null;
                try {
                    document = Jsoup.parse(file, "utf-8");
                    System.out.println("find html file--" + name + "-the dir is-"+file.getPath());
                    processTag(document,"link","href");
                    processTag(document,"script","src");
                    FileOutputStream fos = null;
                    fos = new FileOutputStream(file, false);
                    OutputStreamWriter osw = null;
                    osw = new OutputStreamWriter(fos, "utf-8");
                    osw.write(document.html());
                    osw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        // 对找到的属性值进行链接后面加入版本号的处理
        private void processTag(Document document,String tag,String attr) {
            Elements elements = document.getElementsByTag(tag);
            for (Element element : elements) {
                String attrValue = element.attr(attr);
                attrValue += "?v=" + System.currentTimeMillis();
                System.out.println(attrValue);
                element.attr(attr,attrValue);
            }
        }
    }
    

    最后执行 mvn install,将自定义的插件保存到本地仓库。

    调用自定义插件

    调用自定义插件时,先在pom文件中引入定义插件,然后执行mvn install执行即可。部分代码如下:

    pom文件配置

    <build>
        <plugins>
            <plugin>
                <groupId>com.yanmei.study</groupId>
                <artifactId>yanmei-maven-plugin</artifactId>
                <version>1.0-SNAPSHOT</version>
                <configuration>
                    <dirPath>${project.basedir}\src\main\resources</dirPath>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>yanmeiplugin</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    之后执行mvn clean install 或者mvn install即可。

    demo源码地址:https://github.com/xiaoyuer2012/projectmanager 

    常见maven插件

    记录几个常见的maven插件

    展开全文
  • maven自定义插件

    千次阅读 2018-10-13 15:16:33
    创建一个Maven项目,具体怎么创建略。 将pom.xml文件的packaging 改为maven-plugin &amp;amp;lt;packaging&amp;amp;gt;maven-plugin&amp;amp;lt;/packaging&amp;amp;gt; pom.xml中引入插件依赖...

    例子插件工程https://github.com/zhuquanwen/mvn-plugin.git
    例子调用插件工程

    1. 创建一个Maven项目,具体怎么创建略。

    2. 将pom.xml文件的packaging 改为maven-plugin

      <packaging>maven-plugin</packaging>
      
    3. pom.xml中引入插件依赖

      <dependencies>
              <dependency>
                  <groupId>org.apache.maven.plugin-tools</groupId>
                  <artifactId>maven-plugin-annotations</artifactId>
                  <version>3.5.2</version>
              </dependency>
              <dependency>
                  <groupId>org.apache.maven</groupId>
                  <artifactId>maven-plugin-api</artifactId>
                  <version>3.5.4</version>
              </dependency>
          </dependencies>
      
    4. 定义一个类继承AbstractMojo,实现excute方法

      public class ZqwMojo extends AbstractMojo {
          public void execute() throws MojoExecutionException, MojoFailureException {
              System.out.println("我自定义的插件lalalalala!!!!!!!!!!!!!!!!");
          }
      }
      
    5. 在上面的类上定义Mojo注解,name代表goal,defaultPhase是默认的phase

      @Mojo(name = "zqw", defaultPhase = LifecyclePhase.PACKAGE)
      
    6. 运行mvn clean install 将插件安装到本地仓库,当然也可以deploy到私服

    7. 新建一个模块或工程,pom.xml中引入刚才自定义的插件

       <build>
              <plugins>
                  <plugin>
                      <groupId>com.zqw</groupId>
                      <artifactId>maven-plugin-demo</artifactId>
                      <version>1.0-SNAPSHOT</version>
                  </plugin>
              </plugins>
          </build>
      
    8. 刷新maven或发现在新定义的模块或工程中,有了我们自定义的插件。
      在这里插入图片描述
      运行会发现有了我们刚才System.out.println的输出

    9. 现在这个简单的插件就算完成了,当运行demo:zqw时运行package 这个phase,并输出内容。但是当我们运行mvn package的时候是不会输出的。如果想让package的时候也行我们的插件,要配置如下:

      	<build>
      	        <plugins>
      	            <plugin>
      	                <groupId>com.zqw</groupId>
      	                <artifactId>maven-plugin-demo</artifactId>
      	                <version>1.0-SNAPSHOT</version>
      	                <executions>
      	                    <execution>
      	                        <phase>package</phase>
      	                        <goals>
      	                            <goal>zqw</goal>
      	                        </goals>
      	                    </execution>
      	                </executions>
      	            </plugin>
      	        </plugins>
      	    </build>
      	```
      

    这样运行package就会输出我们的内容了

    1. 向插件中传入参数,在插件类处定义@Paramater

      ```
      @Parameter
          private String msg;
          public void execute() throws MojoExecutionException, MojoFailureException {
              System.out.println(msg);
              System.out.println("共有Java文件数目:" + javaCount());
          }
      ```
      

      在调用处配置参数

      ```
       <build>
              <plugins>
                  <plugin>
                      <groupId>com.zqw</groupId>
                      <artifactId>mvn-plugin</artifactId>
                      <version>1.0-SNAPSHOT</version>
                      <configuration>
                          <msg>lalalalalalala</msg>
                      </configuration>
                      <executions>
                          <execution>
                              <phase>package</phase>
                              <goals>
                                  <goal>count</goal>
                              </goals>
                          </execution>
                      </executions>
                  </plugin>
              </plugins>
          </build>
      ```
      
    2. 插件中传入一个集合参数
      在将@Parameter用集合接收后出现问题

       Execution default-descriptor of goal org.apache.maven.plugins:maven-plugin-plugin:3.2:descriptor failed: 52264
      

      升级一下maven-plugin-plugin的版本就好了

    <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                        <encoding>UTF-8</encoding>
                    </configuration>
                </plugin>
    
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-plugin-plugin</artifactId>
                    <version>3.5.2</version>
                </plugin>
            </plugins>
        </build>
    

    这样就可以定义List的参数了

    ```
    @Parameter(property = "options")
    private String[] options;
    public void execute() throws MojoExecutionException, MojoFailureException {
        System.out.println(msg);
        if (options != null) Arrays.stream(options).forEach(System.out::println);
        System.out.println("共有Java文件数目:" + javaCount());
    }
    ```
    

    调用处

    <build>
            <plugins>
                <plugin>
                    <groupId>com.zqw</groupId>
                    <artifactId>mvn-plugin</artifactId>
                    <version>1.0-SNAPSHOT</version>
                    <configuration>
                        <msg>lalalalalalala</msg>
                        <options>
                            <option>111</option>
                            <option>222</option>
                            <option>333</option>
                            <option>444</option>
    
                        </options>
                    </configuration>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>count</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    
    展开全文
  • Maven插件自定义绑定

    2020-05-17 19:19:28
    除了内置绑定以外,用户还能够自己选择将某个插件目标绑定到生命周期的某个阶段上,这种自定义绑定方式能让Maven项目在构建过程中执行更多更富特色的任务。 一个常见的例子是创建项目的源码jar包。内置的插件绑定...

    除了内置绑定以外,用户还能够自己选择将某个插件目标绑定到生命周期的某个阶段上,这种自定义绑定方式能让Maven项目在构建过程中执行更多更富特色的任务。

    一个常见的例子是创建项目的源码jar包。内置的插件绑定关系中没有涉及这一任务,因此需要用户自行配置。maven-source-plugin可以帮助我们完成该任务,它的jar-no-fork目标能够将项目的主代码打包成jar文件,可以将其绑定到default生命周期的verify阶段上,在执行完集成测试后和安装构件之前创建源码jar包。具体配置见下:

    <plugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-source-plugin</artifactId>
                  <version>2.1.1</version>
                  <executions>
                      <execution>
                          <id>attach-sources</id>
                          <phase>verify</phase>
                          <goals>
                              <goal>jar-no-fork</goal>
                          </goals>
                      </execution>
                  </executions>
              </plugin>

    上述配置中,除了基本的插件坐标声明外,还有插件执行配置,executions下每个execution子元素可以用来配置执行一个任务。

    pom.xml:

    <project xmlns="http://maven.apache.org/POM/4.0.0" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
        http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.amaze</groupId>
      <artifactId>customBindings</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <name>Maven Custom Binding Plugin</name>
      
      <build>
          <plugins>
              <plugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-source-plugin</artifactId>
                  <version>2.1.1</version>
                  <executions>
                      <execution>
                          <id>attach-sources</id>
                          <phase>verify</phase>
                          <goals>
                              <goal>jar-no-fork</goal>
                          </goals>
                      </execution>
                  </executions>
              </plugin>
              
              <plugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-compiler-plugin</artifactId>
                  <configuration>
                      <source>1.6</source>
                      <target>1.6</target>
                  </configuration>
              </plugin>
          </plugins>
      </build>
    </project>

    HelloWorld.java:

    package com.amaze.custombindings;
    
    public class HelloWorld {
    
        public String sayHello(String name){
            return "Hello "+name;
        }
    }

    命令行中到项目根目录下执行mvn clean verify命令,完成后project_home\target下会生成两个jar:

    有很多插件的目标在编写时已经定义了默认绑定阶段,在上述配置中删除<pahse>verify</phase>一行,构建仍然可以顺利完成。可以使用maven-help-plugin查看插件详细信息,了解插件目标的默认绑定阶段,运行命令如下:

    因为输出内容比较多,屏幕放不下,我们将其生成txt文件:

    [INFO] Scanning for projects...
    [INFO]                                                                         
    [INFO] ------------------------------------------------------------------------
    [INFO] Building Maven Stub Project (No POM) 1
    [INFO] ------------------------------------------------------------------------
    [INFO] 
    [INFO] --- maven-help-plugin:2.2:describe (default-cli) @ standalone-pom ---
    [INFO] org.apache.maven.plugins:maven-source-plugin:2.1.1
    
    Name: Maven Source Plugin
    Description: The Maven 2 Source Plugin creates a JAR archive of the source
      files of the current project.
    Group Id: org.apache.maven.plugins
    Artifact Id: maven-source-plugin
    Version: 2.1.1
    Goal Prefix: source
    
    This plugin has 6 goals:
    
    source:aggregate
      Description: Aggregate sources for all modules in an aggregator project.
      Implementation: org.apache.maven.plugin.source.AggregatorSourceJarMojo
      Language: java
      Bound to phase: package
      Before this mojo executes, it will call:
        Phase: 'generate-sources'
    
      Available parameters:
    
        archive
          The archive configuration to use. See Maven Archiver Reference.
    
        attach (Default: true)
          User property: attach
          Specifies whether or not to attach the artifact to the project
    
        excludeResources (Default: false)
          User property: source.excludeResources
          Specifies whether or not to exclude resources from the sources-jar. This
          can be convenient if your project includes large resources, such as
          images, and you don't want to include them in the sources-jar.
    
        excludes
          List of files to exclude. Specified as fileset patterns which are
          relative to the input directory whose contents is being packaged into the
          JAR.
    
        finalName (Default: ${project.build.finalName})
          The filename to be used for the generated archive file. For the
          source:jar goal, '-sources' is appended to this filename. For the
          source:test-jar goal, '-test-sources' is appended.
    
        forceCreation (Default: false)
          User property: source.forceCreation
          Whether creating the archive should be forced. If set to true, the jar
          will always be created. If set to false, the jar will only be created
          when the sources are newer than the jar.
    
        includePom (Default: false)
          User property: source.includePom
          Specifies whether or not to include the POM file in the sources-jar.
    
        includes
          List of files to include. Specified as fileset patterns which are
          relative to the input directory whose contents is being packaged into the
          JAR.
    
        outputDirectory (Default: ${project.build.directory})
          The directory where the generated archive file will be put.
    
        useDefaultExcludes (Default: true)
          Exclude commonly excluded files such as SCM configuration. These are
          defined in the plexus FileUtils.getDefaultExcludes()
    
        useDefaultManifestFile (Default: false)
          Set this to true to enable the use of the defaultManifestFile.
    
    source:help
      Description: Display help information on maven-source-plugin.
        Call
         mvn source:help -Ddetail=true -Dgoal=<goal-name>
        to display parameter details.
      Implementation: org.apache.maven.plugin.source.HelpMojo
      Language: java
    
      Available parameters:
    
        detail (Default: false)
          User property: detail
          If true, display all settable properties for each goal.
    
        goal
          User property: goal
          The name of the goal for which to show help. If unspecified, all goals
          will be displayed.
    
        indentSize (Default: 2)
          User property: indentSize
          The number of spaces per indentation level, should be positive.
    
        lineLength (Default: 80)
          User property: lineLength
          The maximum length of a display line, should be positive.
    
    source:jar
      Description: This plugin bundles all the sources into a jar archive.
      Implementation: org.apache.maven.plugin.source.SourceJarMojo
      Language: java
      Bound to phase: package
      Before this mojo executes, it will call:
        Phase: 'generate-sources'
    
      Available parameters:
    
        archive
          The archive configuration to use. See Maven Archiver Reference.
    
        attach (Default: true)
          User property: attach
          Specifies whether or not to attach the artifact to the project
    
        excludeResources (Default: false)
          User property: source.excludeResources
          Specifies whether or not to exclude resources from the sources-jar. This
          can be convenient if your project includes large resources, such as
          images, and you don't want to include them in the sources-jar.
    
        excludes
          List of files to exclude. Specified as fileset patterns which are
          relative to the input directory whose contents is being packaged into the
          JAR.
    
        finalName (Default: ${project.build.finalName})
          The filename to be used for the generated archive file. For the
          source:jar goal, '-sources' is appended to this filename. For the
          source:test-jar goal, '-test-sources' is appended.
    
        forceCreation (Default: false)
          User property: source.forceCreation
          Whether creating the archive should be forced. If set to true, the jar
          will always be created. If set to false, the jar will only be created
          when the sources are newer than the jar.
    
        includePom (Default: false)
          User property: source.includePom
          Specifies whether or not to include the POM file in the sources-jar.
    
        includes
          List of files to include. Specified as fileset patterns which are
          relative to the input directory whose contents is being packaged into the
          JAR.
    
        outputDirectory (Default: ${project.build.directory})
          The directory where the generated archive file will be put.
    
        useDefaultExcludes (Default: true)
          Exclude commonly excluded files such as SCM configuration. These are
          defined in the plexus FileUtils.getDefaultExcludes()
    
        useDefaultManifestFile (Default: false)
          Set this to true to enable the use of the defaultManifestFile.
    
    source:jar-no-fork
      Description: This goal bundles all the sources into a jar archive. This
        goal functions the same as the jar goal but does not fork the build and is
        suitable for attaching to the build lifecycle.
      Implementation: org.apache.maven.plugin.source.SourceJarNoForkMojo
      Language: java
      Bound to phase: package
    
      Available parameters:
    
        archive
          The archive configuration to use. See Maven Archiver Reference.
    
        attach (Default: true)
          User property: attach
          Specifies whether or not to attach the artifact to the project
    
        excludeResources (Default: false)
          User property: source.excludeResources
          Specifies whether or not to exclude resources from the sources-jar. This
          can be convenient if your project includes large resources, such as
          images, and you don't want to include them in the sources-jar.
    
        excludes
          List of files to exclude. Specified as fileset patterns which are
          relative to the input directory whose contents is being packaged into the
          JAR.
    
        finalName (Default: ${project.build.finalName})
          The filename to be used for the generated archive file. For the
          source:jar goal, '-sources' is appended to this filename. For the
          source:test-jar goal, '-test-sources' is appended.
    
        forceCreation (Default: false)
          User property: source.forceCreation
          Whether creating the archive should be forced. If set to true, the jar
          will always be created. If set to false, the jar will only be created
          when the sources are newer than the jar.
    
        includePom (Default: false)
          User property: source.includePom
          Specifies whether or not to include the POM file in the sources-jar.
    
        includes
          List of files to include. Specified as fileset patterns which are
          relative to the input directory whose contents is being packaged into the
          JAR.
    
        outputDirectory (Default: ${project.build.directory})
          The directory where the generated archive file will be put.
    
        useDefaultExcludes (Default: true)
          Exclude commonly excluded files such as SCM configuration. These are
          defined in the plexus FileUtils.getDefaultExcludes()
    
        useDefaultManifestFile (Default: false)
          Set this to true to enable the use of the defaultManifestFile.
    
    source:test-jar
      Description: This plugin bundles all the test sources into a jar archive.
      Implementation: org.apache.maven.plugin.source.TestSourceJarMojo
      Language: java
      Bound to phase: package
      Before this mojo executes, it will call:
        Phase: 'generate-sources'
    
      Available parameters:
    
        archive
          The archive configuration to use. See Maven Archiver Reference.
    
        attach (Default: true)
          User property: attach
          Specifies whether or not to attach the artifact to the project
    
        excludeResources (Default: false)
          User property: source.excludeResources
          Specifies whether or not to exclude resources from the sources-jar. This
          can be convenient if your project includes large resources, such as
          images, and you don't want to include them in the sources-jar.
    
        excludes
          List of files to exclude. Specified as fileset patterns which are
          relative to the input directory whose contents is being packaged into the
          JAR.
    
        finalName (Default: ${project.build.finalName})
          The filename to be used for the generated archive file. For the
          source:jar goal, '-sources' is appended to this filename. For the
          source:test-jar goal, '-test-sources' is appended.
    
        forceCreation (Default: false)
          User property: source.forceCreation
          Whether creating the archive should be forced. If set to true, the jar
          will always be created. If set to false, the jar will only be created
          when the sources are newer than the jar.
    
        includePom (Default: false)
          User property: source.includePom
          Specifies whether or not to include the POM file in the sources-jar.
    
        includes
          List of files to include. Specified as fileset patterns which are
          relative to the input directory whose contents is being packaged into the
          JAR.
    
        outputDirectory (Default: ${project.build.directory})
          The directory where the generated archive file will be put.
    
        useDefaultExcludes (Default: true)
          Exclude commonly excluded files such as SCM configuration. These are
          defined in the plexus FileUtils.getDefaultExcludes()
    
        useDefaultManifestFile (Default: false)
          Set this to true to enable the use of the defaultManifestFile.
    
    source:test-jar-no-fork
      Description: This goal bundles all the test sources into a jar archive.
        This goal functions the same as the test-jar goal but does not fork the
        build, and is suitable for attaching to the build lifecycle.
      Implementation: org.apache.maven.plugin.source.TestSourceJarNoForkMojo
      Language: java
      Bound to phase: package
    
      Available parameters:
    
        archive
          The archive configuration to use. See Maven Archiver Reference.
    
        attach (Default: true)
          User property: attach
          Specifies whether or not to attach the artifact to the project
    
        excludeResources (Default: false)
          User property: source.excludeResources
          Specifies whether or not to exclude resources from the sources-jar. This
          can be convenient if your project includes large resources, such as
          images, and you don't want to include them in the sources-jar.
    
        excludes
          List of files to exclude. Specified as fileset patterns which are
          relative to the input directory whose contents is being packaged into the
          JAR.
    
        finalName (Default: ${project.build.finalName})
          The filename to be used for the generated archive file. For the
          source:jar goal, '-sources' is appended to this filename. For the
          source:test-jar goal, '-test-sources' is appended.
    
        forceCreation (Default: false)
          User property: source.forceCreation
          Whether creating the archive should be forced. If set to true, the jar
          will always be created. If set to false, the jar will only be created
          when the sources are newer than the jar.
    
        includePom (Default: false)
          User property: source.includePom
          Specifies whether or not to include the POM file in the sources-jar.
    
        includes
          List of files to include. Specified as fileset patterns which are
          relative to the input directory whose contents is being packaged into the
          JAR.
    
        outputDirectory (Default: ${project.build.directory})
          The directory where the generated archive file will be put.
    
        useDefaultExcludes (Default: true)
          Exclude commonly excluded files such as SCM configuration. These are
          defined in the plexus FileUtils.getDefaultExcludes()
    
        useDefaultManifestFile (Default: false)
          Set this to true to enable the use of the defaultManifestFile.
    
    
    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD SUCCESS
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 2.837 s
    [INFO] Finished at: 2015-11-18T11:58:14+08:00
    [INFO] Final Memory: 7M/96M
    [INFO] ------------------------------------------------------------------------

    我们知道,当插件目标被绑定到不同的生命周期阶段的时候,其执行顺序会由生命周期阶段的先后顺序决定。如果多个目标被绑定到同一个阶段,它们的执行顺序会是怎样?答案很简单,当多个插件目标绑定到同一个阶段的时候,这些插件声明的先后顺序决定了目标的执行顺序。

    展开全文
  • Maven 自定义打包

    2021-09-20 15:13:44
    Maven 自定义打包插件介绍需求说明插件使用 Apache Maven Assembly Plugin – Introduction 插件介绍 The Assembly Plugin for Maven enables developers to combine project output into a single distributable ...

    Maven 自定义打包

    Apache Maven Assembly Plugin – Introduction

    插件介绍

    The Assembly Plugin for Maven enables developers to combine project output into a single distributable archive that also contains dependencies, modules, site documentation, and other files

    当我们将项目打包时,希望将项目的说明文档、SQL 脚本文件、项目的配置文件、properties 文件等也打包,此时我们可以通过 Maven 提供的插件 maven-assembly-plugin 进行自定义打包

    目前该插件支持以下打包方式

    zip
    tar
    tar.gz (or tgz)
    tar.bz2 (or tbz2)
    tar.snappy
    tar.xz (or txz)
    jar
    dir
    war
    

    需求说明

    如图所示为项目目录结构,我们现在需要在项目打包时,将 jar 包、application.ymlsql 脚本等打包为一个 zip
    在这里插入图片描述
    最终打包后的 zip 包结构如图,该 zip 包在 mvn install 后会被放置在 target 目录下
    在这里插入图片描述

    在这里插入图片描述

    插件使用

    ① 配置插件依赖

    <build>
      <plugins>
        <plugin>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.2.0</version>
            <executions>
                <execution>
                    <id>make-zip</id>
                    <phase>package</phase>
                    <goals>
                        <goal>single</goal>
                    </goals>
                    <configuration>
                        <descriptors>
                            <!-- 指定配置文件的路径,路径可变,例如:在resources 目录下则为:src/main/resources/assembly.xml -->
                            <descriptor>assembly.xml</descriptor>
                        </descriptors>
                        <archiverConfig>
                            <encoding>utf-8</encoding>
                        </archiverConfig>
                    </configuration>
                </execution>
            </executions>
        </plugin>
      </plugins>
    </build>
    

    ② 配置 assembly.xml

    注意:这里一般容易错的就是路径配置错误,多领会这个配置中的含义,然后先用 IDEAmvn install 后,去项目所在的文件目录去看看,看看需要打包的文件都在哪些目录

    <assembly
            xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
        <!-- 打包文件名的标识符,在打包后的文件名称的后缀显示 -->
        <id>release</id>
        <formats>
            <!-- 这里设置打包的类型,例如 tar、zip 等 -->
            <format>zip</format>
        </formats>
        <includeBaseDirectory>false</includeBaseDirectory>
        <fileSets>
            <!-- 将 jar 包从项目所在的目录中的 target 目录取出来,放到 zip 包-->
            <fileSet>
                <directory>target</directory>
                <includes>
                    <include>${project.artifactId}-${project.version}.jar</include>
                </includes>
                <outputDirectory>/${project.artifactId}</outputDirectory>
            </fileSet>
            <!-- 将 application.yml 从项目所在的目录中的 class 目录取出来,放到 zip 包-->
            <fileSet>
                <directory>${project.build.directory}/classes</directory>
                <includes>
                    <include>application.yml</include>
                </includes>
                <outputDirectory>/${project.artifactId}</outputDirectory>
            </fileSet>
            <!-- 将 scripts 目录中所有文件取出,只排除 run 目录,放到 zip 包的 scripts 目录下-->
            <fileSet>
                <directory>scripts</directory>
                <includes>
                    <include>**</include>
                </includes>
                <excludes>
                    <exclude>run</exclude>
                </excludes>
                <outputDirectory>/${project.artifactId}/scripts</outputDirectory>
            </fileSet>
            <fileSet>
                <directory>./</directory>
                <includes>
                    <include>readme.md</include>
                </includes>
                <outputDirectory>/${project.artifactId}</outputDirectory>
            </fileSet>
        </fileSets>
    </assembly>
    

    在这里插入图片描述
    多试几次,就会实现你的需求!!!

    ③ 打包,执行 mvn install
    在这里插入图片描述

    展开全文
  • Maven引用自定义jar包,并且在打包时包含自定义jar包 1,IDEA或eclipse开发工具使用maven管理项目时,如何引用自定义的jar包呢? 传统项目直接把jar包丢在lib下就ok了,maven项目的话需要在pom文件中配置一下。 第一...
  • 选择一个maven项目作为模板,进入项目根目录 执行:mvn archetype:create-from-project,target/generated-sources/archetype就是我们要的项目了 项目中有些地方需要自己手工替换成变量,工具并没有那么智能。 1)...
  • maven自定义插件

    2020-07-15 19:19:00
    Maven 是一个系统管理框架或体系,专注管理构建的生命周期和各个阶段。真正工作的是绑定到各个阶段的 Maven 插件。每个插件具有一个或一个以上的目标,可以将这些插件的目标绑定到 Maven 生命周期的各个阶段中,或...
  • maven archetype 自定义

    千次阅读 2018-06-06 17:54:06
    archetype是什么 如何创建archetype(简易版,标准版请看后面#更多...archetype是maven的工程原型(模版),也就是说,使用archetype,可以将maven中已存在的工程结构和配置套用到新创建的工程上。 一般用idea创建mav...
  • An archetype is defined asan original pattern or model from which all other things of the same kind are made. 目录 背景 创建archetype模板 手动创建 ...通过已有的项目结构生成...1.通过maven命令的方式 ...
  • Maven引用自定义jar包

    2019-08-09 10:21:51
    在项目中用了一个在Maven中央仓库没有的第三方jar包,Maven中该如何依赖呢? 如果引入不好,启动项目时会报: java.lang.ClassNotFoundException异常 导入jar 在项目src目录同级的目录下,新建lib目录,然后把...
  • 最近在做一些代码生成器,需要在maven生存期执行groovy,使用简单示例,记录一下。 pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=...
  • 使用Maven创建自定义Archetype

    千次阅读 2015-08-25 10:58:07
    问题描述:每次新建Maven Web项目时,Maven自带的maven-archetype-webapp并不好用,其默认的Dynamic Web Module是2.3,Java是1.5,默认的Java版本可以通过配置pom.xml或者settings.xml文件进行修改(修改方法),但...
  • 自定义Maven插件

    2020-05-17 20:32:35
    第一、自定义Maven插件概述 Mojo:Maven plain Old Java Object。每一个Mojo就是Maven中的一个执行目标(executable goal),而插件则是对单个或者多个相关Mojo做统一分发。 一个Mojo包含了一个简单的Java类。插件...
  • 由于公司的项目特殊,生产环境的java源文件(非xml、properties等配置文件)和测试环境的java源文件是不同的,因此每次打包生产环境,普通的maven插件不能够做到先将java源文件拷贝到项目中,然后打包的,尝试了几种...
  • maven简介 Maven 作为一个优秀的项目管理工具,其插件机制为其功能扩展提供了非常大的便捷性。虽然说大多数情况下,我们可能不太会自己去编写 Maven 插件,但不排除在某些特殊的情况下,我们需要去完成一个自己的...
  • 这里写自定义目录标题欢迎使用Markdown编辑器新的改变功能快捷键合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、...
  • maven自定义插件开发

    千次阅读 2017-11-28 10:47:56
    Maven 作为一个优秀的项目管理工具,其插件机制为其功能扩展提供了非常大的便捷性。虽然说大多数情况下,我们可能不太会自己去编写 Maven 插件,但不排除在某些特殊的情况下,我们需要去完成一个自己的插件,来协助...
  • archetype我们可以理解为通过maven创建的项目模板. 1.我们先看看一个普通的项目架构长什么样子: . ├── pom.xml └── src └── main ├── java │ └── cn │ └── com │ ├── HelloWorld...
  • Maven自定义archetype生成项目骨架(一)

    万次阅读 热门讨论 2015-07-15 16:40:45
    ,通过该插件可以自定义符合我们自己需求的archetype。 一:什么是Archetype 总之,原型是一个Maven项目模板工具包。一个原型被定义为从其中相同类型的所有其它事情是由一个原始图案或模型。名称配合,因为我们...
  • Maven 自定义插件实战

    2020-09-24 16:46:10
    使用 IDEA,创建一个org.apache.maven.archetypes:maven-archetype-mojo模版项目,项目名叫demo-maven-plugin。注意,maven 插件的命名有讲究的,官方插件命名格式是maven-pluginName-plugin,非官方插件的命名格式...
  • 教你maven项目如何自定义package打包

    千次阅读 2020-03-13 09:51:20
    插件名称:maven-assembly-plugin 目前最新的版本:3.2.0 作用:该插件可以使用户根据自己的需求自定义打包规则和打包类型 ...org.apache.maven.plugins</groupId> <artifactId>maven-as...
  • SpringBoot官方提供的demo中,pom.xml文件里引用了官方提供的打包插件&lt;build&gt; &lt;plugin&gt; &lt;groupId&gt;org.springframework.boot&...spring-boot-maven-plugin&lt;...
  • 介绍 脚手架即项目模板,使用脚手架的优点:统一规范工程项目、快速创建项目 如我们使用maven的脚手架创建项目,则创建出来的项目结构满足maven的规范 ...org.apache.maven.plugins</groupId> <
  • 1、maven的pom文件和主pom文件要配置相关插件和资源路径编译插件:maven-compiler-plugin资源插件:maven-resources-plugin配置资源路径:resources,指向到相关需要覆盖的路径(properties文件所在位置)[html] ...
  • maven自定义骨架

    2020-08-06 09:43:49
    org.apache.maven.plugins</groupId> <artifactId>maven-archetype-plugin</artifactId> <version>3.0.0</version> </plugin> 三、执行maven 命令, 注意命令的执行目录.
  • 项目中因为webapp目录放到了src同级,所以一直打包加载不到页面,原来maven默认的路径是 src/main/webapp 这样的 ,知道问题就简单了 在pom.xml里面加上配置...org.apache.maven.plugins</groupId> <artifa
  • 自定义maven插件的实现

    万次阅读 2018-06-13 14:22:05
    最近在折腾maven,然后研究了一下maven的插件的写法,然后做了一个案例,通过maven插件来统计当前工程中的目录下有多少个java文件,操作步骤如下,在intellij idea中使用。1、新建一个maven项目,我这边jdk的build...
  • [ERROR] Failed to execute goal org.apache.maven.plugins:maven-plugin-plugin:3.2:descriptor (default -descriptor) on project maven-project: Error extracting plugin descriptor: ‘No mojo definitions wer...
  • 最近做springcloud项目,需要用maven项目管理springboot,eclipse自带的maven模板又贼鸡儿蠢,于是就想自己搞个模板。 网上文章很多,这篇写的挺合我心意 —— maven:从一个已有项目生成一个archetype(未完成) ...

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 29,382
精华内容 11,752
关键字:

mavenplugins自定义