durron597
durron597

Reputation: 32323

Maven clean won't delete copied resources

To make my life easier, I configured my maven to put my deployed application in a different folder (/deploy) than the default (so it doesn't mix with classes/, surefire-reports directories and so forth). It works fine, except that when I try to run mvn clean, it only deletes the jar and copied dependencies, but NOT the copied resources.

UPDATE It appears they are being deleted, but then get placed back immediately. It appears to be related to using Eclipse and Build Automatically, but I'm not sure why changing Maven's configuration would have this effect on Eclipse. END UPDATE

UPDATE 2 At the present moment, none of the answers are correct. This problem clearly has little to do with the deploy directory; it seems that maven-resources-plugin makes Eclipse copy resources as part of Build Automatically. But I'm not sure how to turn this off without stopping using maven-resources-plugin, and without stopping using Build Automatically I will give the bounty to someone who can explain how to do this. END UPDATE 2

Anyway, my directory looks something like this:

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

Here's my pom snippet:

<plugin>
    <artifactId>maven-jar-plugin</artifactId>
    <version>${maven.jar.version}</version>
    <configuration>
        <archive>
            <manifest>
                <mainClass>my.main.Class</mainClass>
                <packageName>my.main</packageName>
                <addClasspath>true</addClasspath>
                <classpathPrefix>lib/</classpathPrefix>
            </manifest>
        </archive>
        <excludes>
            <exclude><!-- the files I don't want in my jar --></exclude>
        </excludes>
        <outputDirectory>${basedir}/deploy</outputDirectory>
    </configuration>
</plugin>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>2.1</version>
    <executions>
        <execution>
            <id>copy-dependencies</id>
            <phase>package</phase>
            <goals>
                <goal>copy-dependencies</goal>
            </goals>
            <configuration>
                <outputDirectory>${basedir}/deploy/lib</outputDirectory>
                <includeScope>compile</includeScope>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <version>2.6</version>
    <executions>
        <execution>
            <id>copy-resources</id>
            <phase>install</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <outputDirectory>${basedir}/deploy</outputDirectory>
                <resources>
                    <resource>
                        <directory>${basedir}/src/main/resources</directory>
                        <filtering>false</filtering>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <artifactId>maven-clean-plugin</artifactId>
    <version>2.5</version>
    <configuration>
        <filesets>
            <fileset>
                <directory>deploy</directory>
                <includes>
                    <include>**/*</include>
                </includes>
                <followSymlinks>false</followSymlinks>
            </fileset>
        </filesets>
    </configuration>
</plugin>

Upvotes: 6

Views: 6109

Answers (7)

Frelling
Frelling

Reputation: 3507

It works fine, except that when I try to run mvn clean, it only deletes the jar and copied dependencies, but NOT the copied resources.

I realize that this is an older question, but it is the first question I found when investigating the same issue experienced by the OP. This is provided as a definitive answer for anyone else engaged in similar literary research as to why this behavior is observed.

It appears they are being deleted, but then get placed back immediately

When run from the command line mvn clean removes all build artifacts by deleting the target directory. However, when run from within Eclipse, it appears that target is not deleted. Furthermore, if non-default resources are defined such as those presented in the question, it appears that they are not deleted.

This is just an illusion and is the result of two distinct tasks. The first, mvn clean deletes the entire target directory. The second task is as a result of Project → Build Automatically causing M2E Builder execution of compile and test-compile phases during full and/or incremental workspace builds.

A prerequisite of these phases is the execution of several other phases such as generate-resources and process-resources (see Lifecycle Reference). These in turn, will cause the execution of resource goals such as those defined by the OP.

The end result of automatic builds is that the target directory is recreated containing:

  • OP's custom resources.
  • test-classes containing compiled code from src/test/java and resources from src/test/resources
  • test-classes containing compiled code and resources from src/test/java and src/test/resources, respectively.

Eclipse does not show classes and test-classes folders since they are marked as output folders (see Properties → Java Build Path → Source). However, since the OP's custom resources are not output directories, they are displayed, resulting in the illusion that mvn clean does not remove them.

If there is doubt, one can verify this by disabling the Java output folders option under Customize View... → Filters in the Project Explorer view. It should be noted that this filtering option is not available in the Package Explorer view. A simpler verification is to run mvn clean and examine the contents of the project directory via the operating system's console or file explorer.

Lastly, running mvn clean from the command line, will verify that it does delete the target directory as expected; only to reappear once Eclipse's automatic build kick in after changes in the IDE. In general, it is a good idea to build projects from the command line once in a while to ensure that the build configuration is IDE agnostic.

Upvotes: 2

Vinay Lodha
Vinay Lodha

Reputation: 2223

It looks like Maven Builder is getting invoked from Eclipse, which is inturn populating files again. I guess you can try disabling Maven builder and Maven nature for your project in eclipse to see if it solves problem.

To disable Eclipse Maven builder this go to

Eclipse Project Properties -> Builders -> Maven builder

Disabling eclipse maven nature enter image description here

All I am saying is problem is due to eclipse-maven integration and not maven

Upvotes: -1

Alexander Kudrevatykh
Alexander Kudrevatykh

Reputation: 870

If question is how to disable eclipse to run specific plugin than you can put in under specific profile that is active by default

<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>

and place ! not-eclipse in profile settings of eclipse project

Upvotes: 2

sErVerdevIL
sErVerdevIL

Reputation: 263

Your issue reproduces on a sample project with resources. Run As -> Maven Clean has the output as mentioned later which only covers the cleaning up.

Although the latest version does not offer this screen, I'm guessing the goal process-resources still runs which would copy the resources to your output directory on running any Maven task.

I wonder why, when the Sonatype book mentions it, the plugin still doesn't show those options.

In a previous project of mine we used to remove the process-resources from Goals to run when updating project configuration to avoid resource processing as major tasks were handled from commandline.

Hope this helps.

P.S: I don't think your issue has anything to do with you changing the deploy directory.

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] ------------------------------------------------------------------------

Upvotes: 1

Nicola Musatti
Nicola Musatti

Reputation: 18218

In your place I'd stick to the "Maven way". If you want a place where only your jar is stored do mvn install and refer to ~/.m2/repository/groupId/artifactId, if you want your jar packed with its configuration files and/or dependencies use the Maven Assembly plugin.

Note that Eclipse's automated rebuilding takes place also with the default configuration. Personally I let Eclipse handle its workspace the way it wants and perform command line builds in a separate check-out or by meeans of Jenkins.

Upvotes: 2

jonathan.cone
jonathan.cone

Reputation: 6706

Stick to the Maven way and put your deploy directory under ${basedir}/target then your problem will fix itself. You can remove the custom plugin configuration for the clean plugin as well.

Upvotes: 7

Ken Hylton
Ken Hylton

Reputation: 1

Add the following fileset to the maven-clean-plugin section

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

Upvotes: 0

Related Questions