如何让自定义的msbuild任务正常工作?

时间:2022-09-02 02:14:30

I have a VS 2008 solution with 2 projects. Project A builds into an assembly that is used by Project B (MVC project). I have created a custom task in Project A, which I call when building Project B.

我有一个包含2个项目的VS 2008解决方案。项目A构建为项目B(MVC项目)使用的程序集。我在项目A中创建了一个自定义任务,我在构建项目B时调用该任务。

I have come across 2 issues:

我遇到过两个问题:

  1. If my AssemblyFile property points to Project A's bin directory, everything works well. But when I want to clean Project B, it always gives the error that Project A's assembly cannot be accessed because it is in use by another process (turns out to be devenv.exe) and the delete fails.
  2. 如果我的AssemblyFile属性指向Project A的bin目录,那么一切都运行良好。但是当我想清理项目B时,它总是会出现错误,即项目A的程序集无法被访问,因为它正被另一个进程使用(原来是devenv.exe)并且删除失败。

  3. If my AssemblyFile property points to Project B's bin directory, build fails saying that it cannot load or find Project A's assembly. I've triple checked the path, filename, uppercase/lowercase, spelling and also checked that Project A's assembly really is there when the build fails.
  4. 如果我的AssemblyFile属性指向Project B的bin目录,则构建失败,说它无法加载或找到Project A的程序集。我已经三次检查了路径,文件名,大写/小写,拼写,还检查了当构建失败时项目A的程序集确实存在。

So what is wrong with scenario 1 and 2. Or where should I be pointing AssemblyFile to? Failing all that, is there a better way to do this?

那么情景1和2有什么问题。或者我应该在哪里指向AssemblyFile?如果做不到这一点,有没有更好的方法呢?

2 个解决方案

#1


If you want to build the two projects independently of each other, then you can't reference the "A" assembly from the VS output directory where it's built to...

如果你想彼此独立地构建这两个项目,那么你就不能从VS输出目录中引用“A”程序集来构建它...

How I would handle this situation:

我将如何处理这种情况:

Create a dependencies folder at the top level of my projects folder in the trunk.. ie:

在trunk中我的projects文件夹的顶层创建一个依赖项文件夹..即:

  • ~/trunk/dependencies
  • ~/trunk/projects
  • ~/trunk/projects/projectA
  • ~/trunk/projects/projectB

Then, in your build task for projectA, copy the assembly to the ~/trunk/dependencies folder once it has been built.

然后,在projectA的构建任务中,将程序集复制到〜/ trunk / dependencies文件夹。

In projectB, reference that assembly in the ~/dependencies folder (so you generate a .refresh file)

在projectB中,在〜/ dependencies文件夹中引用该程序集(因此生成.refresh文件)

This will allow you to clean projectB at will, and the "A" assembly will be brought down whenever you like.

这将允许您随意清理projectB,并且随时可以关闭“A”组件。


For CI, we use JetBrains TeamCity and create artifacts for our builds, then the build tasks references those artifacts as needed.

对于CI,我们使用JetBrains TeamCity并为我们的构建创建工件,然后构建任务根据需要引用这些工件。

#2


There are a few ways to define references on a project.

有几种方法可以定义项目的引用。

  1. A Project Reference - This is a more integrated approach as VS will determine the build order for you. So, when you build Project B, VS determines that Project B depends on Project A and will build Project A first and use the A's output dll from its output location. The drawback to using project references is that when you want to include your Project B in a different solution, you have to also include Project A.

    项目参考 - 这是一种更加集成的方法,因为VS将为您确定构建顺序。因此,当您构建项目B时,VS确定项目B依赖于项目A并将首先构建项目A并从其输出位置使用A的输出dll。使用项目引用的缺点是,当您想要将Project B包含在不同的解决方案中时,还必须包含项目A.

  2. An Assembly Reference - You can also define a reference directly to an assembly. It sound like this is the method you are using. In this case, when you set a file reference from Project B to the ProjA.dll in /bin of Project A, Visual Studio doesn't know that it must first build Project A in order to create the ProjA.dll. You can instruct Visual Studio to first build Project A by setting the proper "Project Dependencies".

    装配参考 - 您还可以直接定义装配的参考。这听起来像是你正在使用的方法。在这种情况下,当您将项目B中的文件引用设置为项目A的/ bin中的ProjA.dll时,Visual Studio不知道它必须首先构建项目A才能创建ProjA.dll。您可以通过设置正确的“项目依赖项”来指示Visual Studio首先构建项目A.

To do this, load up both ProjA and ProjB in a solution. Assuming you already have your assembly reference as you define in your scenario 2, right click project B and select "Project Dependencies...". Here, you just need to place a check next to Project A in the list. Now when you clean and build the solution, VS knows which order to build your projects.

为此,请在解决方案中加载ProjA和ProjB。假设您已在方案2中定义了程序集引用,请右键单击项目B并选择“项目依赖项...”。在这里,您只需要在列表中的项目A旁边打勾。现在,当您清理并构建解决方案时,VS知道构建项目的顺序。

It's also important to note that 'Build Order' and 'Project Dependencies' are defined at the Solution level. So, if you create a different solution with dependent projects defines as file references, you will have to set the build order for that new solution.

同样重要的是要注意“构建顺序”和“项目依赖性”是在解决方案级别定义的。因此,如果您创建一个不同的解决方案,并将依赖项目定义为文件引用,则必须为该新解决方案设置构建顺序。

#1


If you want to build the two projects independently of each other, then you can't reference the "A" assembly from the VS output directory where it's built to...

如果你想彼此独立地构建这两个项目,那么你就不能从VS输出目录中引用“A”程序集来构建它...

How I would handle this situation:

我将如何处理这种情况:

Create a dependencies folder at the top level of my projects folder in the trunk.. ie:

在trunk中我的projects文件夹的顶层创建一个依赖项文件夹..即:

  • ~/trunk/dependencies
  • ~/trunk/projects
  • ~/trunk/projects/projectA
  • ~/trunk/projects/projectB

Then, in your build task for projectA, copy the assembly to the ~/trunk/dependencies folder once it has been built.

然后,在projectA的构建任务中,将程序集复制到〜/ trunk / dependencies文件夹。

In projectB, reference that assembly in the ~/dependencies folder (so you generate a .refresh file)

在projectB中,在〜/ dependencies文件夹中引用该程序集(因此生成.refresh文件)

This will allow you to clean projectB at will, and the "A" assembly will be brought down whenever you like.

这将允许您随意清理projectB,并且随时可以关闭“A”组件。


For CI, we use JetBrains TeamCity and create artifacts for our builds, then the build tasks references those artifacts as needed.

对于CI,我们使用JetBrains TeamCity并为我们的构建创建工件,然后构建任务根据需要引用这些工件。

#2


There are a few ways to define references on a project.

有几种方法可以定义项目的引用。

  1. A Project Reference - This is a more integrated approach as VS will determine the build order for you. So, when you build Project B, VS determines that Project B depends on Project A and will build Project A first and use the A's output dll from its output location. The drawback to using project references is that when you want to include your Project B in a different solution, you have to also include Project A.

    项目参考 - 这是一种更加集成的方法,因为VS将为您确定构建顺序。因此,当您构建项目B时,VS确定项目B依赖于项目A并将首先构建项目A并从其输出位置使用A的输出dll。使用项目引用的缺点是,当您想要将Project B包含在不同的解决方案中时,还必须包含项目A.

  2. An Assembly Reference - You can also define a reference directly to an assembly. It sound like this is the method you are using. In this case, when you set a file reference from Project B to the ProjA.dll in /bin of Project A, Visual Studio doesn't know that it must first build Project A in order to create the ProjA.dll. You can instruct Visual Studio to first build Project A by setting the proper "Project Dependencies".

    装配参考 - 您还可以直接定义装配的参考。这听起来像是你正在使用的方法。在这种情况下,当您将项目B中的文件引用设置为项目A的/ bin中的ProjA.dll时,Visual Studio不知道它必须首先构建项目A才能创建ProjA.dll。您可以通过设置正确的“项目依赖项”来指示Visual Studio首先构建项目A.

To do this, load up both ProjA and ProjB in a solution. Assuming you already have your assembly reference as you define in your scenario 2, right click project B and select "Project Dependencies...". Here, you just need to place a check next to Project A in the list. Now when you clean and build the solution, VS knows which order to build your projects.

为此,请在解决方案中加载ProjA和ProjB。假设您已在方案2中定义了程序集引用,请右键单击项目B并选择“项目依赖项...”。在这里,您只需要在列表中的项目A旁边打勾。现在,当您清理并构建解决方案时,VS知道构建项目的顺序。

It's also important to note that 'Build Order' and 'Project Dependencies' are defined at the Solution level. So, if you create a different solution with dependent projects defines as file references, you will have to set the build order for that new solution.

同样重要的是要注意“构建顺序”和“项目依赖性”是在解决方案级别定义的。因此,如果您创建一个不同的解决方案,并将依赖项目定义为文件引用,则必须为该新解决方案设置构建顺序。