Automatically Refresh Eclipse Projects before Build

The Eclipse CDT build system automatically scans the files in my project folders and adds them to the list of files to be built. That works great if files are added through Eclipse and its plugins: That way Eclipse is notified and aware, and has the files added. But what if I have added files externally (outside of Eclipse)? how can I make Eclipse aware of it?

File added but not shown

File added but not shown in Eclipse Project View

Outline

A new file could have been added by an external script, e.g. to generate new source file with a pre-build step with a time stamp or any other build related information. If that file is always new and does have a different file name, Eclipse is not aware of it. So I need to make sure the Eclipse project ‘knows’ about it before the build. So I need to ‘refresh’ the project or workspace before the build.

In this post I’m using Eclipse Neon 4.6 with the example of MCUXpresso IDE, but any other Eclipse distribution or version can be used.

Refresh Menu

Obviously, there is the ‘Refresh’ menu to manually update the list of files:

Refresh Context menu

Refresh Context menu

And then the new file will show up:

New File After Refresh

New File After Refresh

But this is a manual process. What I would like to do this automatically before every build.

Refresh Policy?

There is the Eclipse ‘Refresh Policy’ feature. Unfortunately, that refresh is only *after* the build:

Eclipse Refresh Policy for Project

Eclipse Refresh Policy for Project

So this does not help.

Workspace Refresh?

There is are two settings in the workspace which usually are not turned on:

Workspace refresh

Workspace refresh

This will do extra polling in the background, or when I access the files/folders in the project view.

It works that way, however because of the polling this adds more load to my host system, especially with many open projects in the workspace. Plus it is for *all* projects in the workspace. What I need is something just for a single project.

Custom Builder for Refresh

The solution I’m using is to add a custom action to the project ‘Builders’ setting:

Force Refresh Action in Builder

Force Refresh Action in Builder

Use the ‘New…’ button to add a new builder action, and use afterwards the ‘Up’ button to have it first in the list so it gets executed before the ‘CDT Builder’:

New Builder Action Program

New Builder Action Program

As program to execute specify something, for a refresh it really does not matter. So it could be any ‘dummy’ program. What I’m using on my side is the ‘echo’ program from the GNU ARM Eclipse build tools suite:

Program

Program

What it does it prints the argument string to the console like this:

Console Output

Console Output

So the program does not matter. What matters are the refresh settings for that action:

Refresh Selected Resource

Refresh Selected Resource

With this, the action (either a dummy action or the action to create a new file with a timestamp) will cause a refresh of the selected (project) folder and all folders inside (recursively). Done :-).

Summary

I love Eclipse because it can do pretty much everything. With the custom builder settings I can do custom actions before (or after) the build without the need to manually write make or build files (but make files are still very, very useful).

Happy Refreshing 🙂

17 thoughts on “Automatically Refresh Eclipse Projects before Build

  1. “I love Eclipse because it can do pretty much everything” – interesting you say this; I found the “walled garden” file system of Eclipse to be about it’s worst feature. In CodeWarrior if I wanted to create a new variant of a project, I just copy the directory; if I want to change files, I just change them. In Eclipse, all these simple file things become a chore, for reasons that I don’t know or see the benefit of.
    Admitted, I did find ways to get around all the barriers, so I suppose it *can* do everything; just maybe not as easily as it should be.

    Like

    • Hmm, I don’t see any ‘walled garden’ file system in Eclipse?
      If I want a new project, then I can simply copy it too (Ctrl-C, Ctrl-V) in Eclipse (or outside Eclipse). It is true that Eclipse uses (as other IDEs) information in .project and .cproject files, but as they are text files, they can be easily managed too. However, what I agree is a constant pain is the separation between ‘Eclipse Framework’ and ‘CDT’ (for C/C++ development): the framework does not know that CDT does and vice-versa. This for example is the source of pain for the debug configurations, where the connection between the editor/framework and the debugger is very thin. Same between build tools and editor. Maybe this is what you observe?

      Like

        • In that case, I recommend that you enable the refresh in the workspace: that way it poll the file system for any changes. That’s the same thing CodeWarrior (classic and Eclipse based) can do too.

          Like

  2. Thank you. It helped me a a lot. There is only one issue. echo.exe cleans console and prints “refreshing project”. Is there any way of appending text on console because I also want to see file sizes?

    Like

  3. Hello Erich,

    sorry to bother you, but I’m dealing with this for quite a bit now.

    I’m trying to automatically increment a constant BUILDNUMBER using a CDT Builder. So I wrote a C# application that find this constant and increment it, but I’d like to only do that if the Build Configuration active is Release, not when it is Debug.

    So, how do I pass and deal with this information at my C# program?

    I see that at my CDT Builder->Main there’s an “Arguments” which I can select “config_name” that is the information I need to pass to my application, but I couldn’t figure it out.

    Thank you very much.

    BR
    Gustavo Costa
    R&D Engineer

    Like

  4. Just for clarification, let’s say I have a shared library project with one source file, zz_dummy_zz.cpp. It’s configured to build using the CDT Builder. I build it, and it’s all fine.

    Now I add a pre-build step in the settings, which is a script that creates a second source file, test.cpp. I add the “Force Refresh” builder as you’ve done above.

    Making sure the project is all cleaned etc, and test.cpp is deleted, I then manually build the project. The output is as follows:

    ====
    12:07:08 **** Build of configuration Debug for project DDSIDL ****
    make pre-build main-build
    Pre-build step description
    /pre-build.sh Debug
    Building file: ../src/zz_dummy_zz.cpp
    Invoking: ARM v7 Linux g++ compiler
    arm-linux-gnueabihf-g++ -Wall -O0 -g3 -c -fmessage-length=0 -MT”src/zz_dummy_zz.o” -fPIC -MMD -MP -MF”src/zz_dummy_zz.d” -MT”src/zz_dummy_zz.o” -o “src/zz_dummy_zz.o” “../src/zz_dummy_zz.cpp”
    Finished building: ../src/zz_dummy_zz.cpp

    Building target: libDDSIDL.so
    Invoking: ARM v7 Linux g++ linker
    arm-linux-gnueabihf-g++ -shared -o “libDDSIDL.so” ./src/zz_dummy_zz.o
    Finished building target: libDDSIDL.so

    12:07:08 Build Finished (took 563ms)
    ====

    Afterwards, the test.cpp file shows up in the project explorer, but there’s no sign of it being built in the output above.

    Are you saying that you DO get files like that one built using this mechanism?

    Like

    • I’m using it differently, as a post-build and not as a pre-build. I think if you do a pre-build, the files for the build are captured before the pre-build, so this is odd and would explain what you see.

      Like

      • Many thanks for replying.

        That’s the behaviour I saw, and was what prompted my question because the paragraph (above)

        “A new file could have been added by an external script, e.g. to generate new source file with a pre-build step with a time stamp or any other build related information. If that file is always new and does have a different file name, Eclipse is not aware of it. So I need to make sure the Eclipse project ‘knows’ about it before the build. So I need to ‘refresh’ the project or workspace before the build.”

        …gives the impression that you were using it in the way I described.

        Like

What do you think?

This site uses Akismet to reduce spam. Learn how your comment data is processed.