我可以在VS中建立这个专案,但无法使用msbuild

时间:2019-12-27 17:20:49

标签: c# .net visual-studio msbuild

我正在尝试在nuget应用中构建ms测试项目

我的项目结构是这样:

commands: [{
        setValueToHere(selector,value) {
           return this
                .setValue(selector,value)
        },
        clickTheButton() {
            return this
                .click('@submitButton');
    }
    }]

如果我打开VS并右键单击并构建MyPackageTest,它会起作用 如果我转到cmd并键入此命令,则会失败:

MyPackage
--> MyPackage
--> MyPackageTest

我收到这样的错误:

msbuild .\MyPackageTest\MyPackageTest.csproj /T:build "/p:Configuration=Release"

编辑:要添加,msbuild也抛出此错误:"C:\Users\me\MyPackage\MyPackageTest\MyPackageTest.csproj" (build target) (1) -> (CoreCompile target) -> MyTests.cs(2,17): error CS0234: The type or namespace name 'VisualStudio' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?) [C:\Users\me\MyPackage\MyPackageTest\MyPackageTest.csproj] MyOtherTests.cs(2,17): error CS0234: The type or namespace name 'VisualStudio' does not exist in the namespace 'Microsoft' (are you missing an assembly reference?) [C:\Users\me\MyPackage\MyPackageTest\MyPackageTest.csproj] MyOtherTests.cs(13,4): error CS0246: The type or namespace name 'TestClassAttribute' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\me\MyPackage\MyPackageTest\MyPackageTest.csproj] MyOtherTests.cs(13,4): error CS0246: The type or namespace name 'TestClass' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\me\MyPackage\MyPackageTest\MyPackageTest.csproj] MyTests.cs(13,4): error CS0246: The type or namespace name 'TestClassAttribute' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\me\MyPackage\MyPackageTest\MyPackageTest.csproj] MyTests.cs(13,4): error CS0246: The type or namespace name 'TestClass' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\me\MyPackage\MyPackageTest\MyPackageTest.csproj] MyOtherTests.cs(17,6): error CS0246: The type or namespace name 'TestMethodAttribute' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\me\MyPackage\MyPackageTest\MyPackageTest.csproj] MyOtherTests.cs(17,6): error CS0246: The type or namespace name 'TestMethod' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\me\MyPackage\MyPackageTest\MyPackageTest.csproj] MyOtherTests.cs(38,6): error CS0246: The type or namespace name 'TestMethodAttribute' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\me\MyPackage\MyPackageTest\MyPackageTest.csproj] MyOtherTests.cs(38,6): error CS0246: The type or namespace name 'TestMethod' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\me\MyPackage\MyPackageTest\MyPackageTest.csproj] MyOtherTests.cs(59,6): error CS0246: The type or namespace name 'TestMethodAttribute' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\me\MyPackage\MyPackageTest\MyPackageTest.csproj] MyOtherTests.cs(59,6): error CS0246: The type or namespace name 'TestMethod' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\me\MyPackage\MyPackageTest\MyPackageTest.csproj] MyOtherTests.cs(75,6): error CS0246: The type or namespace name 'TestMethodAttribute' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\me\MyPackage\MyPackageTest\MyPackageTest.csproj] MyOtherTests.cs(75,6): error CS0246: The type or namespace name 'TestMethod' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\me\MyPackage\MyPackageTest\MyPackageTest.csproj] MyOtherTests.cs(91,6): error CS0246: The type or namespace name 'TestMethodAttribute' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\me\MyPackage\MyPackageTest\MyPackageTest.csproj] MyOtherTests.cs(91,6): error CS0246: The type or namespace name 'TestMethod' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\me\MyPackage\MyPackageTest\MyPackageTest.csproj] MyTests.cs(16,6): error CS0246: The type or namespace name 'TestMethodAttribute' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\me\MyPackage\MyPackageTest\MyPackageTest.csproj] MyTests.cs(16,6): error CS0246: The type or namespace name 'TestMethod' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\me\MyPackage\MyPackageTest\MyPackageTest.csproj] MyTests.cs(36,6): error CS0246: The type or namespace name 'TestMethodAttribute' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\me\MyPackage\MyPackageTest\MyPackageTest.csproj] MyTests.cs(36,6): error CS0246: The type or namespace name 'TestMethod' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\me\MyPackage\MyPackageTest\MyPackageTest.csproj] MyTests.cs(50,6): error CS0246: The type or namespace name 'TestMethodAttribute' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\me\MyPackage\MyPackageTest\MyPackageTest.csproj] MyTests.cs(50,6): error CS0246: The type or namespace name 'TestMethod' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\me\MyPackage\MyPackageTest\MyPackageTest.csproj] MyTests.cs(65,6): error CS0246: The type or namespace name 'TestMethodAttribute' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\me\MyPackage\MyPackageTest\MyPackageTest.csproj] MyTests.cs(65,6): error CS0246: The type or namespace name 'TestMethod' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\me\MyPackage\MyPackageTest\MyPackageTest.csproj] MyTests.cs(81,6): error CS0246: The type or namespace name 'TestMethodAttribute' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\me\MyPackage\MyPackageTest\MyPackageTest.csproj] MyTests.cs(81,6): error CS0246: The type or namespace name 'TestMethod' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\me\MyPackage\MyPackageTest\MyPackageTest.csproj]

2 个答案:

答案 0 :(得分:0)

  

我可以在VS中构建此项目,但不能使用msbuild

首先,如果您使用CMD调用MSBuild,请确保为Environment Variables的路径设置正确的路径。就像C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin(此路径中存在VS2017社区版本和MSBuild.exe)一样。

此外,我假设您有一个名为MyPackageTest的本地项目,该项目由本地VS创建,然后将其迁移到不带VS的构建服务器上。

实际上,此问题可能是由于在迁移到构建服务器期间丢失的缺少软件包引起的。同时,带有命令行的MSBuild 不会还原丢失的软件包,因为您已将其迁移到Build Server中。但是,当您使用VS IDE时,构建过程将在构建过程中自动还原丢失的软件包。(Tools-->Options-->Nuget Package Manager-->General-->Packages Restore)。

建议

1)因此,请首先运行msbuild -t:restore以还原所需的程序包。(首先转到项目路径并执行此操作。)

2)然后在下面输入:

msbuild .\MyPackageTest\MyPackageTest.csproj /T:build "/p:Configuration=Release"

此外,最好使用Build Tool for VS2019Build Tool for VS2017,并请确保构建工具版本与用于创建项目的VS版本相同。

第二,当您使用构建工具时,还要确保已将Testing tools core features安装在vs installer的individual components中。

enter image description here

希望它可以为您提供帮助。

答案 1 :(得分:0)

尝试每次在新的 cmd 窗口中运行 msbuild 作为临时解决方法。

这对我来说每次都有效,也是我目前所做的。我认为运行 msbuild(有或没有 vsdevcmd)会改变一些影响后续 msbuild 运行的环境变量/设置。

不久前我遇到了同样的问题,这极大地影响了我的工作效率。我认为它是在升级到 VS2019 后开始的。我的解决方案不涉及任何 nuget 包,但 msbuild 会因这些错误而随机失败。