简体   繁体   English

如何确保我的 MSBuild 属性尚未在其他地方定义?

[英]How do I ensure my MSBuild properties are not already defined somewhere else?

Is there a way to know if the name for a MSBuild property I want to create is not already being used somewhere else in my solution?有没有办法知道我想要创建的 MSBuild 属性的名称是否尚未在我的解决方案的其他地方使用? How do I create MSBuild customizations that are safe from property name conflicts?如何创建不受属性名称冲突影响的 MSBuild 自定义?

For context, I just spent several hours trying to debug an issue that started after I updated Visual Studio to v17.3: a new NuGet warning was introduced that would alert to duplicate package references being added, and this warning started to fire in a few of my projects even though I didn't (at first glance) have duplicate packages added.对于上下文,我只花了几个小时尝试调试在我将 Visual Studio 更新到 v17.3 后开始的问题:引入了一个新的 NuGet 警告,该警告会警告重复添加 package 引用,并且此警告开始在几个尽管我没有(乍一看)添加了重复的包,但我的项目。

I initially documented the problem here:我最初在这里记录了这个问题:

However, after investigating it further, I found out that the source of the problem was due to me using an existing MSBuild property for our customization that was already defined by another process.但是,在进一步调查之后,我发现问题的根源在于我使用现有的 MSBuild 属性进行自定义,该属性已经由另一个进程定义。 This created a conflict where sometimes the property was true even though I defined it as false , because it was defined as true somewhere else, and I was completely oblivious to it.这造成了冲突,有时即使我将属性定义为false ,该属性也是true ,因为它在其他地方被定义为true ,而我完全没有注意到它。

Once I renamed the property to avoid the conflict, the problem immediately went away.一旦我重命名了属性以避免冲突,问题立即消失了。

When I found out this was the issue, I immediately changed MSBuild log level to Diagnostic , to see if I could find where the property was being defined: if I found it this way, I could use the same approach in the future when creating new variables, so that I wouldn't run into conflicts again.当我发现这是问题所在时,我立即将 MSBuild 日志级别更改为Diagnostic ,以查看是否可以找到定义属性的位置:如果我以这种方式找到它,我可以在将来创建新的时使用相同的方法变量,这样我就不会再次遇到冲突。 This however wasn't effective: I could still not find the existing property anywhere in the build logs.然而这并不有效:我仍然无法在构建日志中的任何地方找到现有属性。

How can I be sure that I'm not inadvertently reusing some property defined elsewhere when coming up with new property names for my own MSBuild customizations?在为我自己的 MSBuild 自定义设置新的属性名称时,如何确保我不会无意中重复使用其他地方定义的某些属性? Similarly, how would external parties ever safely create their properties without risking a conflict with properties already defined in consumer's projects for other purposes?同样,外部各方如何安全地创建他们的属性,而不会冒与消费者项目中已经为其他目的定义的属性发生冲突的风险? Is there such a thing as a "scope" or "namespace" that can be used with MSBuild to ensure isolation of such properties?是否有诸如“范围”或“命名空间”之类的东西可以与 MSBuild 一起使用以确保这些属性的隔离?

You can test if a property is already defined or not.您可以测试一个属性是否已经定义。

<Message Text="MyProperty: $(MyProperty)" Condition="'$(MyProperty)' != ''" />

The following PropertyGroup will set MyProperty when it doesn't already have a value.以下PropertyGroup将在MyProperty还没有值时设置它。 (This example is from MSBuild best practices .) (此示例来自MSBuild 最佳实践。)

<PropertyGroup>
  <MyProperty Condition="'$(MyProperty)' == ''">MyDefaultValue</MyProperty>
</PropertyGroup>

There is no scope or namespace for properties from different sources.不同来源的属性没有 scope 或命名空间。

You can name all your properties (and ItemGroups and Targets) with a specific prefix to reduce the risk of name collisions.您可以使用特定前缀命名所有属性(以及 ItemGroups 和 Targets),以降低名称冲突的风险。 The underscore character is safe to use in property names.下划线字符在属性名称中使用是安全的。 A leading underscore is sometimes used to indicate an 'internal' property but it otherwise uncommon in a property name.前导下划线有时用于表示“内部”属性,但在属性名称中并不常见。 (There is no scope or access protection. It is just a naming convention.) (没有 scope 或访问保护。这只是一个命名约定。)

You could use an underscore as the last character of your prefix.您可以使用下划线作为前缀的最后一个字符。 As an example, if your company were "Acme Corporation" you might use:例如,如果您的公司是“Acme Corporation”,您可以使用:

<PropertyGroup>
  <ACME_MyProperty Condition="'$(MyProperty)' == ''">MyDefaultValue</ACME_MyProperty>
</PropertyGroup>

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

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