22) 插件指定多个source folder、 插件获取 pom 文件中的信息

发布时间 2023-06-01 17:05:45作者: zno2

操作

1. 增加插件

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/generated</outputDirectory>
                            <resources>
                                <resource>
                                    <filtering>true</filtering>
                                    <directory>${basedir}/src/main/templates</directory>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>1.9.1</version>
                <executions>
                    <execution>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>add-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>${project.build.directory}/generated</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

2. 增加模板文件

 在 java/main 下新建 templates 文件夹

 

效果

这两个插件的作用:

build-helper-maven-plugin

可以把某个文件夹指定成为 Source folder

效果等同于

Spring 标准目录结构只有一个 Source Folder ,通过这个插件就可以设置多个。所有source folder 的内容都会被打包的。

 

maven-resources-plugin

 这个可以用来操作 java文件,本例中是用的 copy ,且copy过程中 过滤("@version@" ,在java文件中使用这个即可截获pom文件的<version>标签的文本内容)  

java 模板示例文件:

package a.b;
public class PomInfo {
    
    private String artifactId = "@artifactId@";
    private String groupId = "@groupId@";
    private String version = "@version@";

}

 

生成的类:

package a.b;
public class PomInfo {
    
    private String artifactId = "z-test";
    private String groupId = "cn.zno";
    private String version = "0.0.1-SNAPSHOT";

}

 

 

 

.
│   pom.xml
│
│
├───src
│   ├───main
│   │   ├───java
│   │   ├───resources
│   │   └───templates
│   │       └───a
│   │           └───b
│   │                   PomInfo.java
│   │
│   └───test
│       ├───java
│       └───resources
└───target
    ├───classes
    │   ├───a
    │   │   └───b
    │   │           PomInfo.class
    │   │
    │   └───META-INF
    │       │   MANIFEST.MF
    │       │
    │       └───maven
    │           └───cn.zno
    │               └───z-test
    │                       pom.properties
    │                       pom.xml
    │
    ├───generated
    │   └───a
    │       └───b
    │               PomInfo.java
    │
    └───test-classes