简体   繁体   English

自定义任务MSBuild

[英]Custom task MSBuild

I created a .dll file that parses WSDL me file and saves it to other files. 我创建了一个.dll文件来解析WSDL我的文件并将其保存到其他文件。 File is called WsdlParser.dll. 该文件称为WsdlParser.dll。 Input parameters are: the input file , output file ,the element and parent element. 输入参数为:输入文件,输出文件,元素和父元素。 I need to build at another program to call this my dll and enter the data for MSBuild(I need to build at another program to create those files from WSDL).Method parser me to create new files. 我需要在另一个程序中进行构建以调用此dll并输入MSBuild的数据(我需要在另一个程序中进行构建以从WSDL创建那些文件)。方法解析器以创建新文件。 I have created a project where I put reference Microsoft.Build.Framework , Microsoft.Build.Utulities , WsdlParser. 我创建了一个项目,在其中引用了Microsoft.Build.Framework,Microsoft.Build.Utulities,WsdlParser。

  namespace MyParserBuild
{
    public class ParserClass : Task
    {
        private string input;
        private string output;
        private string element;
        private string parentElement;

        public override bool Execute()
        {
            try
            {
                WsdlParser.Parser parse = new WsdlParser.Parser(input, output);
                parse.Parse(parentElement, element);
            }

            catch (Exception e)
            {
                Console.WriteLine(e);
                return false;
            }
            return true;
        }

        /*  Properties  */
        [Required]
        public string Input
        {
            get { return input; }
            set { input = value; }
        }

        [Required]
        public string Output
        {
            get { return output; }
            set { output = value; }
        }

        [Required]
        public string Element
        {
            get { return element; }
            set { element = value; }
        }

        [Required]
        public string ParentElement
        {
            get { return parentElement; }
            set { parentElement = value; }
        }
    }
}

And I create an XML file named MyBuild.targets 然后创建一个名为MyBuild.targets的XML文件

    <?xml version="1.0" encoding="utf-8" ?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <UsingTask TaskName="MyParserBuild.ParserClass" AssemblyFile="C:\Work\Common code\MyCode\MyParserBuild\MyParserBuild\bin\Debug\MyParserBuild.dll"/>

  <!--Variable-->
  <PropertyGroup>
    <PG_Input>C:\Work\Common code\MyCode\MyParserBuild\MyParserBuild\MyWSDLFile2.wsdl</PG_Input>
    <PG_Output>C:\Users\Administrator\Desktop\test</PG_Output>
    <PG_Element>schema</PG_Element>
    <PG_ParentElement>types</PG_ParentElement>
  </PropertyGroup>

  <Target Name="PreBuild">
    <ParserClass Input="$(PG_Input)"
                 Output="$(PG_Output)"
                 Element="$(PG_Element)"
                 ParentElement="$(PG_ParentElement)" />
  </Target>
</Project>

Finally, I put this target into .csproj another program. 最后,我将此目标放入另一个程序.csproj中。

<Import Project="C:\Work\Common code\MyCode\MyParserBuild\MyParserBuild\MyBuild.targets"/>

My problem is that when I build another program will not create those files from WSDL. 我的问题是,当我构建另一个程序时,将无法从WSDL创建这些文件。

You only imported the project, but you did not add anything which will make the target get called: it's not because the target is named 'PreBuild' that it will be used as pre build event. 您仅导入了项目,但未添加任何使目标被调用的内容:不是因为目标名为“ PreBuild”,所以它将用作预构建事件。 To do that there are a couple of options, here are two of them: 为此,有两个选项,其中有两个:

  1. Modify the project file to call the PreBuild Target, for instance add this et the end of the file: 修改项目文件以调用PreBuild Target,例如,在文件末尾添加以下内容:

     <Target Name="BeforeBuild" DependsOnTargets="PreBuild"> </Target> 
  2. Use a PreBuildEvent call the target PreBuild. 使用PreBuildEvent调用目标PreBuild。 A pre build event is something which runs like on the commandline. 预构建事件是类似于命令行运行的事件。 In Visual Studio, right-click the project, select Properties and then Build Events. 在Visual Studio中,右键单击项目,选择“属性”,然后选择“生成事件”。 Enter 输入

     msbuild MyBuild.targets /t:PreBuild 

    In this case you do not need to import the MyBuild.targets in your project. 在这种情况下,您不需要在项目中导入MyBuild.targets。

Note: it is not a good idea to use absolute paths like C:\\Work\\Common code\\MyCode\\... . 注意:使用绝对路径(例如C:\\Work\\Common code\\MyCode\\... )不是一个好主意。 If you'd copy your code to another location everything would break. 如果将代码复制到另一个位置,一切都会中断。 better figure out a directory structure you'll stick with and use relative paths within that structure, or make everything parameterized like $(CommonCodePath)\\MyBuild.targets and pass CommonCodePath as a property to msbuild or so. 更好地找出您将坚持使用的目录结构,并在该结构中使用相对路径,或者对所有参数进行参数化,例如$(CommonCodePath)\\MyBuild.targets并将CommonCodePath作为属性传递给msbuild左右。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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