locked
Environment Variable properties and <Import> error handling RRS feed

  • Question

  • Hello,

       I'm currently working on an nmake-to-msbuild project and trying to mimick a few of the behaviors of nmake is proving difficult. I'd like to do is set an environment variable to an MSBuild property...

     

    <PropertyGroup>
    
        <myProp>$(my_env_var)</myProp>
    
    </PropertyGroup>

    ...and my_env_var will be a directory path, but to ensure compatability with my MSBuild project, I'd like to remove all quotations, if any, before (or after) setting it to myProp.  On a related issue, I'm doing an <Import> using this same environment variable directly under my properties.  However, if the environment variable is not set, instead of getting:

    error MSB4019: The imported project "d:\include\common\msbuild.otherproject.xml" was not found. Confirm that the path in the <Import> declaration is correct, and that the file exists on disk.

    I would like to throw a an error, similiar to what can be done with the <Error> tag inside a target, to inform them that they need to set their environment variable.

    Any thoughts/ideas?

    Friday, April 23, 2010 2:49 PM

Answers

  • Hi,

    1. To remove quotes from env. variable or other MSBuild property you can use MSBuild functions, a new feature in 4.0, more info on http://blogs.msdn.com/msbuild/.  I'd think of using String.TrimXXX methods.
    2. To throw an error, you could use Message task with a condition, some text and importance=high or Error task with a condition and some text
    3. To check if the project being imported exists before the import, you can use a condition attribute, like
      <Import Project="$(SomeProjectPath)" Condition="Exists('$(SomeProjectPath)')"/>

    Cheers,
    Ilya
     

    • Proposed as answer by Ilya V Biryukov Friday, April 23, 2010 7:16 PM
    • Marked as answer by Chao Kuo Friday, April 30, 2010 2:25 AM
    Friday, April 23, 2010 7:13 PM