Maven clean不会删除复制的资源

时间:2013-08-26 17:50:34

标签: java eclipse maven

为了让我的生活更轻松,我将我的maven配置为将我已部署的应用程序放在与默认文件夹不同的文件夹(/deploy)中(因此它不会与classes/混合,{{1}目录等等)。它运行正常,只是当我尝试运行surefire-reports时,它只删除jar并复制依赖项,但不删除复制的资源。

更新它们似乎正在被删除,但随后立即被放回。它似乎与使用Eclipse和mvn clean有关,但我不确定为什么更改Maven的配置会对Eclipse产生这种影响。 END UPDATE

更新2 目前,没有一个答案是正确的。这个问题显然与Build Automatically目录没什么关系;似乎 maven-resources-plugin 使Eclipse复制资源成为自动构建的一部分。但我不确定如何在不停止使用 maven-resources-plugin 的情况下关闭此功能,并且不停止使用自动构建我将奖励给可以解释的人这该怎么做。 END UPDATE 2

无论如何,我的目录看起来像这样:

deploy

这是我的my-app |-- pom.xml |-- src | |-- main | | |-- java | | `-- resources | | |-- script.sh | | `-- config | | `-- app.properties | `-- test | |-- java | `-- resources `-- deploy |-- my-app.jar <----- This gets deleted correctly |-- lib <----- This gets deleted correctly | |-- dependency1.jar <----- This gets deleted correctly | |-- dependency2.jar <----- This gets deleted correctly |-- config <----- This DOES NOT get deleted correctly | `-- app.properties <----- This DOES NOT get deleted correctly `-- script.sh <----- This DOES NOT get deleted correctly 摘要:

pom

7 个答案:

答案 0 :(得分:7)

坚持使用Maven方式并将deploy目录放在${basedir}/target下,然后您的问题就会解决。您也可以删除clean插件的自定义插件配置。

答案 1 :(得分:2)

在你的位置,我会坚持“Maven方式”。如果你想要一个只存放你的jar的地方做mvn install并参考~/.m2/repository/groupId/artifactId,如果你想要你的jar包含其配置文件和/或依赖项,请使用Maven Assembly plugin

请注意,Eclipse的自动重建也使用默认配置。就个人而言,我让Eclipse以其想要的方式处理其工作区,并在单独的签出或Jenkins的海洋中执行命令行构建。

答案 2 :(得分:2)

如果问题是how to disable eclipse to run specific plugin,则可以在默认情况下处于活动状态的特定配置文件中输入

<project>
  ...
  <profiles>
    <profile>
      <id>not-eclipse</id>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>
      <build>
        <plugins>
          <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.6</version>
             ...
        </plugins>
      </build>
  </profile>
  ...
</project>

并将! not-eclipse放在eclipse项目的配置文件设置中

答案 3 :(得分:1)

您的问题会在包含资源的示例项目中重现。运行方式 - &gt; Maven Clean具有后面提到的输出,仅涵盖清理。

虽然最新版本不提供此screen,但我猜测目标process-resources仍会运行,这会在运行任何Maven任务时将资源复制到输出目录。

我想知道为什么,当Sonatype本书提到它时,该插件仍然没有显示这些选项。

在我之前的一个项目中,我们曾经从目标中删除process-resources以便在更新项目配置时运行以避免资源处理,因为主要任务是从命令行处理的。

希望这有帮助。

P.S:我认为您的问题与更改部署目录无关。

Apache Maven 3.0.4 (r1232337; 2012-01-17 14:14:56+0530)
Maven home: C:\projects\workspace\abc\EMBEDDED
Java version: 1.7.0_04, vendor: Oracle Corporation
Java home: C:\Java\jre7
Default locale: en_US, platform encoding: Cp1252
OS name: "windows 7", version: "6.1", arch: "amd64", family: "windows"
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
[INFO] Error stacktraces are turned on.
[DEBUG] Reading global settings from EMBEDDED\conf\settings.xml
[DEBUG] Reading user settings from C:\Users\stackoverflow\.m2\settings.xml
[DEBUG] Using local repository at C:\Users\stackoverflow\.m2\repository
[DEBUG] Using manager EnhancedLocalRepositoryManager with priority 10 for C:\Users\stackoverflow\.m2\repository
[INFO] Scanning for projects...
[DEBUG] Extension realms for project abc:abc:jar:0.0.1-SNAPSHOT: (none)
[DEBUG] Looking up lifecyle mappings for packaging jar from ClassRealm[plexus.core, parent: null]
[DEBUG] === REACTOR BUILD PLAN ================================================
[DEBUG] Project: abc:abc:jar:0.0.1-SNAPSHOT
[DEBUG] Tasks:   [clean]
[DEBUG] Style:   Regular
[DEBUG] =======================================================================
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building Test 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[DEBUG] Lifecycle default -> [validate, initialize, generate-sources, process-sources, generate-resources, process-resources, compile, process-classes, generate-test-sources, process-test-sources, generate-test-resources, process-test-resources, test-compile, process-test-classes, test, prepare-package, package, pre-integration-test, integration-test, post-integration-test, verify, install, deploy]
[DEBUG] Lifecycle clean -> [pre-clean, clean, post-clean]
[DEBUG] Lifecycle site -> [pre-site, site, post-site, site-deploy]
[DEBUG] Lifecycle default -> [validate, initialize, generate-sources, process-sources, generate-resources, process-resources, compile, process-classes, generate-test-sources, process-test-sources, generate-test-resources, process-test-resources, test-compile, process-test-classes, test, prepare-package, package, pre-integration-test, integration-test, post-integration-test, verify, install, deploy]
[DEBUG] Lifecycle clean -> [pre-clean, clean, post-clean]
[DEBUG] Lifecycle site -> [pre-site, site, post-site, site-deploy]
[DEBUG] === PROJECT BUILD PLAN ================================================
[DEBUG] Project:       abc:abc:0.0.1-SNAPSHOT
[DEBUG] Dependencies (collect): []
[DEBUG] Dependencies (resolve): []
[DEBUG] Repositories (dependencies): [central (http://repo.maven.apache.org/maven2, releases)]
[DEBUG] Repositories (plugins)     : [central (http://repo.maven.apache.org/maven2, releases)]
[DEBUG] -----------------------------------------------------------------------
[DEBUG] Goal:          org.apache.maven.plugins:maven-clean-plugin:2.4.1:clean (default-clean)
[DEBUG] Style:         Regular
[DEBUG] Configuration: <?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <directory default-value="${project.build.directory}"/>
  <excludeDefaultDirectories default-value="false">${clean.excludeDefaultDirectories}</excludeDefaultDirectories>
  <failOnError default-value="true">${maven.clean.failOnError}</failOnError>
  <followSymLinks default-value="false">${clean.followSymLinks}</followSymLinks>
  <outputDirectory default-value="${project.build.outputDirectory}"/>
  <reportDirectory default-value="${project.reporting.outputDirectory}"/>
  <skip default-value="false">${clean.skip}</skip>
  <testOutputDirectory default-value="${project.build.testOutputDirectory}"/>
  <verbose>${clean.verbose}</verbose>
</configuration>
[DEBUG] =======================================================================
[INFO] 
[INFO] --- maven-clean-plugin:2.4.1:clean (default-clean) @ abc ---
[DEBUG] Created new class realm maven.api
[DEBUG] Importing foreign packages into class realm maven.api
[DEBUG]   Imported: org.apache.maven.wagon.events < plexus.core
...
Lots of such lines
...
[DEBUG]   Imported: org.codehaus.plexus.* < plexus.core
[DEBUG]   Imported: org.codehaus.plexus.personality < plexus.core
[DEBUG] Populating class realm maven.api
[DEBUG] org.apache.maven.plugins:maven-clean-plugin:jar:2.4.1:
[DEBUG]    org.apache.maven:maven-plugin-api:jar:2.0.6:compile
[DEBUG]    org.codehaus.plexus:plexus-utils:jar:2.0.5:compile
[DEBUG] Created new class realm plugin>org.apache.maven.plugins:maven-clean-plugin:2.4.1
[DEBUG] Importing foreign packages into class realm plugin>org.apache.maven.plugins:maven-clean-plugin:2.4.1
[DEBUG]   Imported:  < maven.api
[DEBUG] Populating class realm plugin>org.apache.maven.plugins:maven-clean-plugin:2.4.1
[DEBUG]   Included: org.apache.maven.plugins:maven-clean-plugin:jar:2.4.1
[DEBUG]   Included: org.codehaus.plexus:plexus-utils:jar:2.0.5
[DEBUG]   Excluded: org.apache.maven:maven-plugin-api:jar:2.0.6
[DEBUG] Configuring mojo org.apache.maven.plugins:maven-clean-plugin:2.4.1:clean from plugin realm ClassRealm[plugin>org.apache.maven.plugins:maven-clean-plugin:2.4.1, parent: sun.misc.Launcher$AppClassLoader@769fe666]
[DEBUG] Configuring mojo 'org.apache.maven.plugins:maven-clean-plugin:2.4.1:clean' with basic configurator -->
[DEBUG]   (f) directory = C:\projects\rollbase2.0workspace\abc\target
[DEBUG]   (f) excludeDefaultDirectories = false
[DEBUG]   (f) failOnError = true
[DEBUG]   (f) followSymLinks = false
[DEBUG]   (f) outputDirectory = C:\projects\rollbase2.0workspace\abc\target\classes
[DEBUG]   (f) reportDirectory = C:\projects\rollbase2.0workspace\abc\target\site
[DEBUG]   (f) skip = false
[DEBUG]   (f) testOutputDirectory = C:\projects\rollbase2.0workspace\abc\target\test-classes
[DEBUG] -- end configuration --
[DEBUG] Skipping non-existing directory C:\projects\rollbase2.0workspace\abc\target
[DEBUG] Skipping non-existing directory C:\projects\rollbase2.0workspace\abc\target\classes
[DEBUG] Skipping non-existing directory C:\projects\rollbase2.0workspace\abc\target\test-classes
[DEBUG] Skipping non-existing directory C:\projects\rollbase2.0workspace\abc\target\site
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.693s
[INFO] Finished at: Sun Sep 01 16:38:36 IST 2013
[INFO] Final Memory: 4M/121M
[INFO] ------------------------------------------------------------------------

答案 4 :(得分:1)

  

它工作正常,但是当我尝试运行mvn clean时,它只删除jar并复制依赖项,但不删除复制的资源。

我意识到这是一个较老的问题,但这是我在调查OP遇到的同一问题时发现的第一个问题。对于参与类似文学研究的其他人来说,这是一个明确的答案,为什么会出现这种行为。

  

看来它们正在被删除,但随后立即被放回

从命令行运行时mvn clean删除target目录,删除所有构建工件。但是,从Eclipse中运行时,似乎不会删除target。此外,如果定义了非默认资源(例如问题中显示的资源),则看起来它们不会被删除。

这只是一种幻觉,是两个截然不同的任务的结果。第一个,mvn clean删除整个target目录。第二项任务是项目→自动构建的结果,导致M2E Builder在完整和/或增量工作区构建期间执行compiletest-compile阶段。

这些阶段的先决条件是执行其他几个阶段,例如generate-resourcesprocess-resources(请参阅Lifecycle Reference)。反过来,这将导致执行资源目标,例如由OP定义的资源目标。

自动构建的最终结果是重新创建target目录,其中包含:

  • OP的自定义资源。
  • test-classes包含来自src/test/java的已编译代码和来自src/test/resources
  • 的资源
  • test-classes分别包含来自src/test/javasrc/test/resources的已编译代码和资源。

Eclipse不会显示classestest-classes个文件夹,因为它们被标记为输出文件夹(请参阅属性→Java构建路径→源)。但是,由于OP的自定义资源不是输出目录,因此会显示它们,从而导致错误mvn clean不会删除它们。

如果有疑问,可以通过在项目浏览器视图中的自定义视图...→过滤器下禁用Java output folders选项来验证这一点。应该注意, Package Explorer 视图中没有此过滤选项。更简单的验证是运行mvn clean并通过操作系统的控制台或文件浏览器检查项目目录的内容。

最后,从命令行运行mvn clean将验证它是否按预期删除了target目录;只有在IDE更改后自动构建才会重新出现。通常,偶尔从命令行构建项目是个好主意,以确保构建配置与IDE无关。

答案 5 :(得分:0)

将以下文件集添加到maven-clean-plugin部分

<fileset>
    <directory>${basedir}/deploy/config</directory>
</fileset>

答案 6 :(得分:-1)

看起来Maven Builder正在从Eclipse调用,这是再次填充文件。我想你可以尝试在eclipse中为你的项目禁用Maven构建器和Maven性质,看它是否能解决问题。

要禁用Eclipse Maven构建器,请转到

Eclipse Project Properties -> Builders -> Maven builder

禁用eclipse maven性质 enter image description here

我所说的只是问题是由于eclipse-maven整合而不是maven