当我使用Maven构建项目时,我想在每个构建时创建一个新文件夹。
低于我的要求:
问题:
答案 0 :(得分:2)
以下是如何完成它的示例:
${maven.build.timestamp}
属性作为目录名后缀,以使动态目录名具有时间戳maven.build.timestamp.format
以删除可能导致某些操作系统出现问题的:
默认字符。例如,值yyyy-MM-dd_HH-mm-ss
(_
替换:
)maven-clean-plugin
以清除latest
文件夹。官方文档已经提供了how to delete additional files maven-resources-plugin
将项目资源复制到latest
和新的构建商店文件夹。官方文档已经提供了how to copy resources maven-jar-plugin
将项目工件打包到两个目录(最新版本和构建版本),我建议使用两个不同的maven classifier
(时间戳和latest
是完美的候选者)。官方文档alread提供了how to attach additional jars maven-dependency-plugin
将项目依赖项复制到两个文件夹。官方文档已经提供了how to copy project dependencies 以上是上面的完整实现,已经放在一个配置文件中:
<profile>
<id>build-store</id>
<properties>
<maven.build.timestamp.format>yyyy-MM-dd_HH-mm-ss</maven.build.timestamp.format>
<build.store.directory>c:\build_${maven.build.timestamp}</build.store.directory>
<build.store.directory.latest>c:\latest</build.store.directory.latest>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<filesets>
<fileset>
<directory>${build.store.directory.latest}</directory>
<includes>
<include>**/*</include>
</includes>
</fileset>
</filesets>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<executions>
<execution>
<id>copy-dependencies-to-build-store</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<outputDirectory>${build.store.directory}</outputDirectory>
<classifier>${maven.build.timestamp}</classifier>
</configuration>
</execution>
<execution>
<id>copy-dependencies-to-latest</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<outputDirectory>${build.store.directory.latest}</outputDirectory>
<classifier>latest</classifier>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.10</version>
<executions>
<execution>
<id>copy-dependencies-to-build-store</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${build.store.directory}/libs</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
<execution>
<id>copy-dependencies-to-latest</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${build.store.directory.latest}/libs</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<id>copy-resources-build-store</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${build.store.directory}/conf</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
<execution>
<id>copy-resources-latest</id>
<phase>package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${build.store.directory.latest}/conf</outputDirectory>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
通过以下方式调用构建:
mvn clean package -P build-store
然后清理latest
文件夹内容,根据构建时间戳创建新文件夹,将所需文件移动到latest
文件夹。
您可以定义目标文件夹,修改build.store.directory
和build.store.directory.latest
属性。
根据进一步的要求,可以轻松地将其他改进和配置添加到此配置文件中。