How can I get MSBUILD to evaluate and print the full path when given a relative path?

前端 未结 5 825
情话喂你
情话喂你 2020-12-13 17:05

How can I get MSBuild to evaluate and print in a task an absolute path given a relative path?

Property Group

5条回答
  •  时光说笑
    2020-12-13 17:42

    Wayne is correct that well-known metadata does not apply to properties - only to items. Using properties such as "MSBuildProjectDirectory" will work, but I'm not aware of a built in way to resolve the full path.

    Another option is to write a simple, custom task that will take a relative path and spit out the fully-resolved path. It would look something like this:

    public class ResolveRelativePath : Task
    {
        [Required]
        public string RelativePath { get; set; }
    
        [Output]
        public string FullPath { get; private set; }
    
        public override bool Execute()
        {
            try
            {
                DirectoryInfo dirInfo = new DirectoryInfo(RelativePath);
                FullPath = dirInfo.FullName;
            }
            catch (Exception ex)
            {
                Log.LogErrorFromException(ex);
            }
            return !Log.HasLoggedErrors;
        }
    }
    

    And your MSBuild lines would look something like:

    
        D:\BuildTasks\Build.Tasks.dll
        ..\..\..\Public\Server\
        c:\Program Files (x86)\Program\
    
    
    
    
        
        
        
        
    
    

提交回复
热议问题