maven的一些使用技巧

定义时间戳

  1. 简单做法
    maven自带的时间戳${maven.build.timestamp}
    修改自带时间戳的格式,在pom.xml中添加属性:
<properties>
    <maven.build.timestamp.format>yyyy-MM-dd HH:mm:ss</maven.build.timestamp.format>
</properties>

这个时间戳的时间是标准的UTC时间,没有时区的概念,想要特定时区的时间只能利用插件
2. 高级用法(使用插件)

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.9.1</version>
    <executions>
        <execution>
            <id>timestamp-property</id>
            <!-- 该插件定义了很多阶段,这里指定的阶段表示在那个阶段才会产生一个时间戳属性 -->
            <phase>prepare-package</phase>
            <goals>
                <goal>timestamp-property</goal>
            </goals>
            <configuration>
                <!-- 自定义的时间戳属性的变量名 -->
                <name>current.time</name>
                <!-- 时间戳样式 -->
                <pattern>yyyy-MM-dd HH:mm:ss</pattern>
                <!-- 指定的时区 -->
                <timeZone>GMT+8</timeZone>
            </configuration>
        </execution>
    </executions>
</plugin>

prepare-package阶段,会生成一个current.time属性的常量,可以自定义时区

自定义MANIFEST.MF

修改打的jar包里的MANIFEST.MF的内容

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
    <version>3.0.1</version>
    <configuration>
        <archive>
            <manifest>
                <addClasspath>false</addClasspath>
            </manifest>
             <!-- manifestEntries的内容是一个map结构,也就是要添加的属性说明,原来的属性不变 -->
            <manifestEntries>
                <!-- 添加一个Built-Time属性的条目,内容就是上面定义的当前时间 -->
                <!-- 结合上面自定义的时间戳,就可以取到定义的时间戳,注意自定义时间戳的phase是prepare-package -->
                <Built-Time>
                    ${current.time}
                </Built-Time>
            </manifestEntries>
        </archive>
    </configuration>
</plugin>

添加其他的源文件目录

指定额外的源文件目录,默认只有src/main/java,使用插件实现

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.9.1</version>
    <executions>
        <execution>
            <id>add-source</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>add-source</goal>
            </goals>
            <configuration>
                <sources>
                    <source>src</source>
                    <source>org</source>
                    <source>dict</source>
                </sources>
            </configuration>
        </execution>
    </executions>
</plugin>

问题

在eclipse中,build-path设置中指定的编译输出目录和maven中配置的不一致,会出现奇怪的现象,比如,eclipse中指定源文件夹resources的输出目录是target/classes,但在pom.xml配置中,不对resources目录做任何处理,.java以外的文件都将会“编译到”target/classes目录,.java的文件将不会编译,所以结论就是,maven项目在eclipse中使用时,最好只要在pom.xml中指定,pom.xml中指定各个目录的作用。

修改maven项目结构,怎么去定义pom.xml,修改哪些属性,利用哪些插件?有时间好好总结一下,然后完成这儿。。。。。

原文地址:https://www.cnblogs.com/catelina/p/10827498.html