Throw an Error in an MSBuild Task - msbuild

How do you throw an error from within an MSBuild task and force the build to fail. Something like:
<Task>
<ThrowError Condition="$(SomeCondition)" Message="There was a problem with the build" />
</Task>

Use the Error Task
<Error Condition="$(SomeCondition)" Text="There was a problem with the build" />

The Error-task would do the trick.
<Error
Text="errormessage"
Condition="errorcondition" />

Have you tried Error instead of ThrowError ? It works for me ;)

Related

Retrieving the nupkg version number in msbuild

I've modified a csproj file in VS2017 to create a Nuget package when my .NET 4.5 project is release-built. The next automation step is to add this package to my private feed on a network share. Here are the commands I'm using:
<Exec Command="nuget.exe pack -Properties "Configuration=Release" -Symbols $(ProjectName).csproj" />
<Exec Command="nuget.exe add $(ProjectName).$(ProductVersion).nupkg -source \\MYSERVER\Nuget packages" />
Line 1 works and produces a nupkg file of the form productname.nn.nn.nn.nn.
However line 2 is not returning a value for the ProductVersion token (which was a guess on my part).
I've struggled to find a reference for MSBUILD tokens (that in itself would be useful to know), but what I really need to know is the correct MSBUILD token/variable/property for the the version - and that is the same value as the generated Nuget package.
I explored $(PackageVersion) suggested by Martin Ullrich, but it's not going to work with older projects even with Nuget 4.10 installed. Also I couldn't get Troopers sample to work how I wanted and I ended up with a variation on one of the posts here. I adapted it to give me the assembly version as opposed to the file version.
<UsingTask TaskName="GetVersionParts" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<AssemblyPath ParameterType="System.String" Required="true" />
<MajorVersion ParameterType="System.Int32" Output="true" />
<MinorVersion ParameterType="System.Int32" Output="true" />
<BuildVersion ParameterType="System.Int32" Output="true" />
</ParameterGroup>
<Task>
<Using Namespace="System.Reflection" />
<Code Type="Fragment" Language="cs">
<![CDATA[
Version v = AssemblyName.GetAssemblyName(this.AssemblyPath).Version;
this.MajorVersion = v.Major;
this.MinorVersion = v.Minor;
this.BuildVersion = v.Build;
]]>
</Code>
</Task>
</UsingTask>
<Target Name="AfterBuild" Condition=" '$(Configuration)' == 'Release'">
<Message Text="**** After-build process starting ****" />
<Exec Command="nuget.exe pack -Properties "Configuration=Release" -Symbols $(ProjectName).csproj" />
<GetVersionParts AssemblyPath="$(OutputPath)$(AssemblyName).dll">
<Output TaskParameter="MajorVersion" PropertyName="MajorVersionNumber" />
<Output TaskParameter="MinorVersion" PropertyName="MinorVersionNumber" />
<Output TaskParameter="BuildVersion" PropertyName="BuildVersionNumber" />
</GetVersionParts>
<Exec Command="nuget.exe add $(MSBuildProjectName).$(MajorVersionNumber).$(MinorVersionNumber).$(BuildVersionNumber).nupkg -source "\\My feed server\Shared location"" />
<Exec Command="move *.symbols.nupkg "\\My feed server\Shared location\Symbols"" />
<Message Text="**** After-build process completed ****" />
</Target>
Although this works, I can't help feeling that it should be easier.
Further reading
https://learn.microsoft.com/en-us/visualstudio/msbuild/msbuild-inline-tasks
https://learn.microsoft.com/en-us/visualstudio/msbuild/common-msbuild-project-properties
https://learn.microsoft.com/en-us/nuget/tools/nuget-exe-cli-reference#add
You could get the assembly version with this macro. I call it before the postbuild event for example
<!--Macro to get the version -->
<Target Name="PostBuildMacros">
<GetAssemblyIdentity AssemblyFiles="$(TargetPath)">
<Output TaskParameter="Assemblies" ItemName="CurrentAssembly" />
</GetAssemblyIdentity>
<ItemGroup>
<VersionNumber Include="%(CurrentAssembly.Version)" />
</ItemGroup>
</Target>
<!-- override PostBuildEvent to call PostBuildMacros -->
<PropertyGroup>
<PostBuildEventDependsOn>
$(PostBuildEventDependsOn);
PostBuildMacros;
</PostBuildEventDependsOn>
<PostBuildEvent>
...
</PostBuildEvent>
</PropertyGroup>

how to use dirSize() EL function in oozie decision node

I tried to use dirSize() in oozie decision node. but it does not work.
When I use ${fs:dirSize(InputDir) gt 10 * KB} in the decision node. The oozie workflow status moves to failed state and no error is displayed.
Please find below the code snippet from workflow.
<action name="L1_check" cred="hcat_creds">
<hive xmlns="uri:oozie:hive-action:0.2">
<job-tracker>${jobTracker}</job-tracker>
<name-node>${nameNode}</name-node>
<script>l1_check_code.sql</script>
<param>PI_DB=${PI_DB}</param>
<param>PRD_DB=${PRD_DB}</param>
<param>PROMO_START_DATE=${PROMO_START_DATE}</param>
<param>PROMO_END_DATE=${PROMO_END_DATE}</param>
</hive>
<ok to="decision-node" />
<error to="fail" />
</action>
<decision name="decision-node">
<switch>
<case to="L1_exists">
${fs:dirSize(InputDir) gt 10 * KB}
</case>
<default to="fail"/>
</switch>
</decision>
InputDir is a defined in the properties file and the path exists.
First action L1_check executes and transitioned to decision node. then workflow status changes to Failed state.
Is there any error in the way function is used? If yes, what should be right function to use?
Also is there a way to give a path with combination of parameter and string
eg: dirSize(InputDir/l1_check)
where InputDir is a parameter and l1_check is a static name

NLog fails to open file on Windows with Mono

I have a small set of ServiceStack REST services that is using NLog 2.1 (from NuGet) for logging.
My test server is running:
Windows 7
IIS 7.5
.NET 4.5
NLog config:
<nlog throwExceptions="true" internalLogToConsole="true" internalLogLevel="Debug" xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target name="c" xsi:type="Console" />
<target name="f1" xsi:type="File" fileName="C:\logs\test.log" />
</targets>
<rules>
<logger name="*" writeTo="c,f1" />
</rules>
</nlog>
My NLog config is exceedingly simple... just trying to get it working...
and in this configuration, everything works fine. NLog creates the log files correctly.
On my DEVELOPMENT machine, I am using:
Windows 7
Xamarin Studio / XSP4
Mono 3.2.3
Here is my Application_Start...
protected void Application_Start() {
LogManager.LogFactory = new NLogFactory();
ILog log = LogManager.GetLogger(typeof(Global));
log.Info("Application_Start called");
try {
new AppHost().Init();
} catch (Exception e) {
log.Error("Exception caught initializing AppHost");
}
}
In this configuration, my service's AppHost().Init() throws an exception as ServiceStack is registering my services in ServiceController.cs. I believe that part is irrelevant except that it is the first time something is logged outside of Application_Start (because both of the calls in Application_Start work... the log.info before the exception and the log.error after the exception).
Here is the exception that is shown:
The most relevant bit is that there was a System.NotImplementedException thrown at NLog.Internal.FileAppenders.BaseFileAppender.WindowsCreateFile (System.String fileName, Boolean allowConcurrentWrite).
I have found a workaround (in the accepted answer below). Hopefully anyone else who runs into this will quickly come upon this solution.
Some digging around on Google led me to this NLog pull request:
Avoid Win32-specific file functions in Mono where parts not implemented.
It appears that this change tries to use the preprocessor to NOT call WindowsCreateFile at all. However, for some reason, this still executes.
So I then went to check the newest version of BaseFileAppender.cs in the NLog GitHub repository to make sure someone didn't at some later point break this again.
#if !NET_CF && !SILVERLIGHT && !MONO
try
{
if (!this.CreateFileParameters.ForceManaged && PlatformDetector.IsDesktopWin32)
{
return this.WindowsCreateFile(this.FileName, allowConcurrentWrite);
}
}
catch (SecurityException)
{
InternalLogger.Debug("Could not use native Windows create file, falling back to managed filestream");
}
#endif
Hmmm... it's still there. What gives? Why doesn't MONO seem to be defined by the preprocessor, thus allowing this block to execute? I'm not sure. Before I started down that path of investigation, I noticed another change...
if (!this.CreateFileParameters.ForceManaged && ...
So after following that tracing that ForceManaged boolean back to it's origin, it appeared that I could set forceManaged="true" on my FileTarget declaration in my NLog config. Could it really be that simple?!
<nlog throwExceptions="true" internalLogToConsole="true" internalLogLevel="Debug" xmlns="http://www.nlog-project.org/schemas/NLog.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<targets>
<target name="c" xsi:type="Console" />
<target name="f1" xsi:type="File" forceManaged="true" fileName="C:\logs\test.log" />
</targets>
<rules>
<logger name="*" writeTo="c,f1" />
</rules>
</nlog>
Once that change was made, everything worked... the call to WindowsCreateFile that was throwing the exception was skipped & a managed filestream was used instead, which works great. Hallelujah!
Lastly, I could not find that forceManaged flag in the NLog documentation anywhere. Would likely have found this solution sooner if it was.

MSBuild UsingTask Resolve References

I feel like I've fixed this before, but I can't remember how.
I have a tasks file that looks like this (CustomTasks.tasks):
<UsingTask AssemblyFile="CustomTasks.dll" TaskName="MyCustomTask"/>
it references an assembly (namely Ionic.Zip.dll). Ionic.Zip.dll is not in the GAC (and I don't want it to be). It sits right next to my CustomTasks.dll.
I have a directory called MSBuild one level up from my sln file which has CustomTasks.tasks, CustomTasks.dll and Ionic.Zip.dll in it.
I have a csproj that references the tasks file and calls the custom task:
<Import Project="$(ProjectDir)\..\MSBuild\CustomTasks.tasks" />
<MyCustomTask ..... />
at build time, this yields:
The "MyCustomTask" task could not be loaded from the assembly ....MyCustomTasks.dll. Could not load file or assembly 'Ionic.Zip,......' or one of its dependencies.
Got tired and frustrated and took a direct approach...I don't think this is the same way I solved the problem previously...but maybe this will help someone else. Other, more elegant solutions are more than welcome.
<Target Name="BeforeBeforeBuild" BeforeTargets="BeforeBuild">
<HandleAssemblyResolve SearchPath="$(ProjectDir)\..\MSBuild\" />
</Target>
<UsingTask TaskName="HandleAssemblyResolve" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<SearchPath ParameterType="System.String" Required="true" />
</ParameterGroup>
<Task>
<Using Namespace="System" />
<Using Namespace="System.IO" />
<Using Namespace="System.Reflection" />
<Code Type="Fragment" Language="cs">
<![CDATA[
AppDomain.CurrentDomain.AssemblyResolve += (sender, e) =>
{
var assemblySearchPath = Path.Combine(SearchPath, e.Name.Split(',')[0]);
if (File.Exists(assemblySearchPath)) return Assembly.LoadFrom(assemblySearchPath);
return null;
};
]]>
</Code>
</Task>
</UsingTask>
This is actually easy to fix. Put your custom build tasks and dependencies in a different folder. Then dependencies are loaded correctly.
For example like so:
<UsingTask AssemblyFile="..\BuildTools\CustomTasks.dll" TaskName="MyCustomTask"/>

Trying to use MSBuild task <Xml.ModifyFile> from Microsoft.Sdc.Common.tasks

I have a problem with the Xml.ModifyFile task which I do not understand. Can you guys help?
My goal is simply to manipulate an attribute in an xml document.
Im fairly new to the world of xml and especially msbuild hence I how a hard time interpreting the error message i am receiving. It seems to me that my build file is valid so I guess something is wrong in sdc.tasks dll file.
As it can be seen from the build file I have added a target called "ping" for the sake of testing. That target works with the sdc.task Ping without any problems
Can you guys suggest a fix or an alternative solution to the challenge of modifying xml files with msbuild.
An additional question - how do one declare multiple namespaces as argument to the Xml.ModifyFile sdc.task? The explanation of the namespace attribute is as follows:
An array of TaskItems specifiying "Prefix" and "Uri" attributes for use with the specified xPath. I have tried to find an explanation or example of the usage of taskitems but unfortunately without any luck.
thanks / derdres
I will list the following below:
build file
the xml file that I try to modify
the error message
1) build file
<Target Name="Go">
<CallTarget Targets="modify"></CallTarget>
<!--<CallTarget Targets="ping"></CallTarget>-->
</Target>
<Target Name="modify">
<Xml.ModifyFile
Path="C:\Users\Andreas\Desktop\MSBuild\Test_05_april\Test01\bookstore_adv.xml"
AttributeName="age"
Force="true"
XPath="/bookstore/book[#id=2]/#age"
NewValue="200"
ShowMatches="Yes"
>
</Xml.ModifyFile>
<Message Text="After modification"></Message>
</Target>
<!--<Target Name="ping">
<Ping
Machine="localhost"
Count="2"
Interval="1000"
Timeout="3000"
BufferSize="1024"
AllowFragmentation="false"
TimeToLive="128"
StopOnSuccess="true"
LogSuccess="true">
<Output TaskParameter="FailureCount" PropertyName="FailedPingCount" />
<Output TaskParameter="RoundTripTime" PropertyName="RoundTripDuration" />
</Ping>
<Message Text="FailedPingcount: $(FailedPingCount)"></Message>
<Message Text="RoundTripDuration: $(RoundTripDuration)"></Message>
</Target>-->
2) xml file
<?xml version="1.0" encoding="utf-8"?>
<!--<bookstore xmlns:hat="www.google.dk/hat" xmlns:briller="www.google.dk/briller">-->
<!--<bookstore xmlns:hat="www.google.dk/hat">-->
<bookstore>
<book id="1">
<title>Harry Potter</title>
<author>Rowling</author>
</book>
<book id="2" age="100">
<title>Lykke Per</title>
<author>Pontoppidan</author>
</book>
3) Build Error Message
Build FAILED.
"C:\Users\Andreas\Desktop\MSBuild\Test_05_april\Test01\Deploy.proj" (default target) (1) ->
(modify target) ->
C:\Users\Andreas\Desktop\MSBuild\Test_05_april\Test01\Deploy.proj(11,9): error : A task error has occured.\r
C:\Users\Andreas\Desktop\MSBuild\Test_05_april\Test01\Deploy.proj(11,9): error : Message = Object reference not set to
an instance of an object.\r
C:\Users\Andreas\Desktop\MSBuild\Test_05_april\Test01\Deploy.proj(11,9): error : Action = Replace\r
C:\Users\Andreas\Desktop\MSBuild\Test_05_april\Test01\Deploy.proj(11,9): error : Path = C:\Users\Andreas\Desktop\MS
Build\Test_05_april\Test01\bookstore_advanced.xml\r
C:\Users\Andreas\Desktop\MSBuild\Test_05_april\Test01\Deploy.proj(11,9): error : Namespace = <null>\r
C:\Users\Andreas\Desktop\MSBuild\Test_05_april\Test01\Deploy.proj(11,9): error : XPath = /bookstore/book[#id=2]/#age
\r
C:\Users\Andreas\Desktop\MSBuild\Test_05_april\Test01\Deploy.proj(11,9): error : RegularExpression = <String.Empty>\r
C:\Users\Andreas\Desktop\MSBuild\Test_05_april\Test01\Deploy.proj(11,9): error : NewValue = 200\r
C:\Users\Andreas\Desktop\MSBuild\Test_05_april\Test01\Deploy.proj(11,9): error : AttributeName = age\r
C:\Users\Andreas\Desktop\MSBuild\Test_05_april\Test01\Deploy.proj(11,9): error : Force = True\r
C:\Users\Andreas\Desktop\MSBuild\Test_05_april\Test01\Deploy.proj(11,9): error : TreatNewValueAsXml = False\r
C:\Users\Andreas\Desktop\MSBuild\Test_05_april\Test01\Deploy.proj(11,9): error : ShowMatches = Yes\r
C:\Users\Andreas\Desktop\MSBuild\Test_05_april\Test01\Deploy.proj(11,9): error : \r
C:\Users\Andreas\Desktop\MSBuild\Test_05_april\Test01\Deploy.proj(11,9): error : at Microsoft.Sdc.Tasks.Xml.ModifyFile.Interna
lExecute() in c:\projects\codeplex\sdctasks\Solutions\Main\Tasks\Xml\ModifyFile.cs:line 346\r
C:\Users\Andreas\Desktop\MSBuild\Test_05_april\Test01\Deploy.proj(11,9): error : at Microsoft.Sdc.Tasks.TaskBase.Execute() in
c:\projects\codeplex\sdctasks\Solutions\Main\Tasks\TaskBase.cs:line 66
0 Warning(s)
1 Error(s)
Time Elapsed 00:00:00.20
In your XPath you are searching for the attribute age /bookstore/book[#id=2]/#age but in your task you set the AttributeName to "age". So it is like you want the attribute age of the attribute age.
You just have to change your XPath to /bookstore/book[#id=2] to make it work.
<Target Name="modify">
<Xml.ModifyFile
Path="C:\Users\Andreas\Desktop\MSBuild\Test_05_april\Test01\bookstore_adv.xml"
AttributeName="age"
Force="true"
XPath="/bookstore/book[#id=2]"
NewValue="200"
ShowMatches="Yes">
</Xml.ModifyFile>
<Message Text="After modification"/>
</Target>
How do one declare multiple namespaces as argument to the Xml.ModifyFile sdc.task?
<ItemGroup>
<Namespace Include="www.google.dk/briller">
<Prefix>briller</Prefix>
<Uri>www.google.dk/briller</Uri>
</Namespace>
<Namespace Include="www.google.dk/hat">
<Prefix>hat</Prefix>
<Uri>www.google.dk/hat</Uri>
</Namespace>
</ItemGroup>
<Target Name="modify">
<Xml.ModifyFile
Path="C:\Users\Andreas\Desktop\MSBuild\Test_05_april\Test01\bookstore_adv.xml"
AttributeName="age"
Force="true"
XPath="/bookstore/book[#id=2]"
NewValue="200"
ShowMatches="Yes"
Namespace="#(Namespace)">
</Xml.ModifyFile>
<Message Text="After modification"/>
</Target>
Your XML File Is Invalid.
I Tried It On My Machine , And It Worked Fine.
Just Close The bookstore Tag.