【问题标题】:Maven clean failing to delete filesMaven clean 无法删除文件
【发布时间】:2018-05-18 18:37:12
【问题描述】:

尝试配置 maven clean 插件以删除我项目中的其他目录,但它无法正常工作。这是使用 maven 3.5 进行的简单复制。

开始使用简单的演示应用:https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html

修改pom.xml配置maven-clean-plugin

<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.mycompany.app</groupId>
    <artifactId>my-app</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>Maven Quick Start Archetype</name>
    <url>http://maven.apache.org</url>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-clean-plugin</artifactId>
                <version>3.0.0</version>
                <executions>
                    <execution>
                        <id>auto-clean</id>
                        <phase>clean</phase>
                        <goals>
                            <goal>clean</goal>
                        </goals>
                        <configuration>
                            <verbose>true</verbose>
                            <fieldsets>
                                <fieldset>
                                    <directory>${basedir}</directory>
                                    <includes>
                                        <include>demo/**</include>
                                    </includes>
                                </fieldset>
                            </fieldsets>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.8.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

创建一个名为demo的虚拟目录

 mkdir demo && touch demo/hello

现在运行:

 mvn package
 mvn clean

您会注意到demo 目录仍然存在。我不知道这是为什么。我已经阅读了这些 SO 文章:

我已经尝试运行目标、阶段和 id 的不同排列:

  • clean
  • clean:clean
  • clean:auto-clean

等等都无济于事……

我做了一个简单的健全性检查以查看传递给 mojo 的内容:

[DEBUG] Configuring mojo org.apache.maven.plugins:maven-clean-plugin:3.0.0:clean from plugin realm ClassRealm[plugin>org.apache.maven.plugins:maven-clean-plugin:3.0.0, parent: sun.misc.Launcher$AppClassLoader@55f96302]
[DEBUG] Configuring mojo 'org.apache.maven.plugins:maven-clean-plugin:3.0.0:clean' with basic configurator -->
[DEBUG]   (f) directory = /Users/djthomps/Desktop/test/target
[DEBUG]   (f) excludeDefaultDirectories = false
[DEBUG]   (f) failOnError = true
[DEBUG]   (f) followSymLinks = false
[DEBUG]   (f) outputDirectory = /Users/djthomps/Desktop/test/target/classes
[DEBUG]   (f) reportDirectory = /Users/djthomps/Desktop/test/target/classes
[DEBUG]   (f) retryOnError = true
[DEBUG]   (f) skip = false
[DEBUG]   (f) testOutputDirectory = /Users/djthomps/Desktop/test/target/test-classes
[DEBUG]   (f) verbose = true
[DEBUG] -- end configuration --

看起来我的配置选项从未设置过。为什么?

注意:我也阅读了插件文档:https://maven.apache.org/plugins/maven-clean-plugin/examples/delete_additional_files.html。据我了解,我想做的事情是可能的。

【问题讨论】:

    标签: java maven maven-clean-plugin


    【解决方案1】:

    也许尝试使用fileset 配置,而不是fieldset,如下所示:

    <configuration>
        <followSymLinks>false</followSymLinks>
        <filesets>
            <fileset>
                <directory>*all_this_directory*</directory>
            </fileset>
            <fileset>
                <directory>*only_some_of_this_directory*</directory>
                <includes>
                    <include>file1_with_wildcard*</include>
                    <include>file2</include>
                    <!--continue the same...-->
                </includes>
            </fileset>
        </filesets>
    </configuration>
    

    【讨论】:

    • 哇,这毕竟是一个错字:s
    • 哦,哈哈,我什至没有查看 fieldset 是否作为配置存在,我以为是!我只记得我使用了文件集。如果你能接受我的回答那就太好了:)
    【解决方案2】:

    可能是您在 Eclipse 中选择了单线程,所以您遇到了这个问题。它发生在我身上。我在 2 周后解决了这个问题,所以想与你分享。

    1. 右键单击项目并选择 Run As -> Maven build..
    2. 将打开一个窗口,然后提供目标全新安装(不带 mvn)。
    3. 在同一窗口中选择多个线程。

    它对我有用:)

    编码愉快!

    【讨论】:

      【解决方案3】:

      我想不通。清洁目标只是无法清理我的输出/目标目录。我只是更改了默认输出/目标目录

      <profiles>
      <profile>
        <id>myprofile</id>
        <build>
              <directory>target1</directory>
          </build>
        <activation>
        <activeByDefault>true</activeByDefault>
        </activation>      
      </profile>
      

      【讨论】:

        猜你喜欢
        • 2014-11-07
        • 2011-01-15
        • 1970-01-01
        • 2016-04-19
        • 2018-05-28
        • 2021-08-30
        • 2012-11-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多