简体   繁体   中英

Is there a way to hide Maven 2 “target/” folder in Eclipse 3?

I'm using maven 2.0.9 with Eclipse 3.3.2.

I'm used to launching a fresh build once per day by a mvn clean install . Then, if I refresh my Eclipse project, it will be "polluted" by files from Maven's target directory.

That's very annoying while performing searches, getting resources by "open resource" and so on.

Is there a way to avoid Eclipse looking in this folder?

右键单击要忽略的文件夹,打开“属性”对话框,选择“资源”选项卡,然后选中“派生”复选框。

Reconfigure "clean" in Maven not to delete target directory:

<plugin>
    <artifactId>maven-clean-plugin</artifactId>
    <configuration>
        <excludeDefaultDirectories>true</excludeDefaultDirectories>
        <filesets>
            <!-- delete directories that will be generated when you 
                 start the develpment server/client in eclipse  
            -->
            <fileset>
                <directory>target</directory>
                <includes>
                    <include>**/*</include>
                </includes>
            </fileset>
        </filesets>
    </configuration>
</plugin>

(found at: http://maven.40175.n5.nabble.com/how-to-NOT-delete-target-dir-td3389739.html#a3413930 )

Solution for Indigo [SR2]

Project Explorer > Customize View > Filters > [*] Maven Build Folder

I have been so pissed by this problem that I wrote a plugin to solve it. You get get the source and jar from:

https://github.com/YA2O/Eclipse_Plugin_Target_Derivator

To solve this problem here is what I did:

  • Install Groovy Monkey for Eclipse
  • Created a Bean Shell Script "UpdateMavenDerived_Beanshell.gm" to mark any directory named target as derived.
  • -----------Cut below here for script--------------

    /*
     * Menu: Find System Prints > Beanshell
     * Script-Path: /GroovyMonkeyScripts/monkey/UpdateMavenDerived_Beanshell.gm
     * Kudos: Bjorn Freeman-Benson & Ward Cunningham & James E. Ervin
     * License: EPL 1.0
     * LANG: Beanshell
     * DOM: http://groovy-monkey.sourceforge.net/update/plugins/net.sf.groovyMonkey.dom
     */
    out.println("Setting target directories to derived status.");
    var projects = workspace.getRoot().getProjects();
    for ( var i = 0; i < projects.length; i++) {
        var project = projects[i];
        if (project.isOpen()) {
            out.println("Project: " + project.getName());
            var members = project.members();
            for ( var j = 0; j < members.length; j++) {
                if (members[j].getName().equals("target")) {
                    out.println("setting derived status on: "+ members[j].getFullPath());
                    members[j].setDerived(true);
                }
            }
        }
    }
    

    Did you try configuring the "Java Element Filters" option dialog box, (through the top-right arrow of the project explorer) ?

    If needed, you can define your own ViewerFilter

    Preferences > Team > Ignored Resources

    Add "target", and restart Eclipse.

    The maven plugin does not hide away the target directory. It does however use the maven target folders to configure eclipse. So target/classes and target/test-classes are used by eclipse, and eclipse filters these folders out. This is done by "mvn eclipse:eclipse" as well as by the m2eclipse plugin. What is left visible is everything in the target directory besides these two folders (the generated jar file for example).

    You can create a filter for the package explorer, but that will not influence the "open resource".

    I had some issues because some solutions here only work for some views, so I made an illustrated summary.

    Tested in Eclipse 4.4 (Luna), should work like this since 3.7 according to other answers here.

    Package Explorer

    Package Explorer View MenuFilters... → check Name filter patterns and input target .

    Be careful, this will hide all folders and files named target , not just the default maven build directory! The Project Explorer view has a better option without this issue.

    Package Explorer菜单包资源管理器对话框

    Project Explorer

    Project Explorer View Menu → Custom View... → search for "maven" and check Maven build folder .

    Checking this option hides the build directory as defined in the projects pom.xml configuration.

    Project Explorer菜单Project Explorer对话框

    Are you using the Maven plugin for Eclipse?

    I would imagine it would hide some of the 'pollution' for you.

    It would also allow you to perform the build within Eclipse - meaning it would refresh the project view for you at the same time.

    Maven 2 Eclipse

    Using Resource filter looks solve issue if you using 'mvn clean install' frequently.

    Basically it will add 'target' to and sub folders to exclude folder directly to .project file. After run this script, if you import project to eclipse, you can see the setting 'Project'->'Properties'->'Resource'->'Resource Filters'. Before this whenever I run mvn clean install, eclipse took 50% of my CPU but now it stay under 5% while build project.


    function eclipse-setup() {
         mvn eclipse:clean
         mvn eclipse:eclipse
         #maven target folders
         find . -name .project -exec sed -i.MSORG 's/<\/projectDescription>/<filteredResources> <filter> <id>1314376338264<\/id> <name><\/name> <type>26
    <\/type> <matcher> <id>org.eclipse.ui.ide.multiFilter<\/id> <arguments>1.0-name-matches-false-false-target<\/arguments> <\/matcher> <\/filter> <filt
    er> <id>1314387234341<\/id> <name><\/name> <type>6<\/type> <matcher> <id>org.eclipse.ui.ide.multiFilter<\/id> <arguments>1.0-name-matches-false-fals
    e-*.cache.html<\/arguments> <\/matcher> <\/filter> <\/filteredResources><\/projectDescription>/' {} \;
    
    }
    

    When Eclipse freezes, looking at the process activity, I can see it browsing all my target, .hg and .git directories. Moreover, those directories are also copied into Eclipse's bin directory. A lot of CPU and disk usage for nothing.

    Not cleaning the target directory is not an acceptable solution.

    There was a solution using a Monkey script (http://maven.40175.n5.nabble.com/Eclipse-amp-target-directory-td72354.html) but the project has been closed.

    I still look for a solution to tell Eclipse to ignore Maven target directories, as well as .hg and .git directories.

    Eclipse continuously watching those is a pain.

    I also use m2eclipse but it doesn't solve that issue.

    The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM