UnitTask error trying to use Microsoft.Build.Evaluation - visual-studio

have the following code inside my CSProj file:
<UsingTask TaskName="HelloWorld" TaskFactory="CodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<Task>
<!--Microsoft.Build.dll-->
<Using Namespace="Microsoft.Build.Evaluation" />
<Code Type="Fragment" Language="cs">
<![CDATA[
var p = new Project("$(MSBuildProjectFullPath)");
]]>
</Code>
</Task>
</UsingTask>
in my AfterBuild I call it like this:
<Target Name="AfterBuild">
<HelloWorld />
</Target>
The error I am getting is:
error CS0246: The type or namespace name 'Project' could not be found
(are you missing a using directive or an assembly reference?)
Referring to var p = new Project

Include Microsoft.Build reference in the task can fix that issue. (The Microsoft.Build.dll reference need to be added to your project firstly)
<Task>
<Reference Include="Microsoft.Build"/>
<Using Namespace="Microsoft.Build.Evaluation" />
<Code Type="Fragment" Language="cs">
<![CDATA[
var p = new Project("$(MSBuildProjectFullPath)");
]]>
</Code>
</Task>

Related

Change base path on VS publish

I have a blazorWASM project. While developing, I need index.html to have it's base path set to <base href="/" />, but when I use the built-in publish to file, the output is set to land directly into my PHP project which acts as a host that serves WASM static files, and needs this base path: <base href="/wwwroot/" />.
Is there a way to have them automatically switched so I do not keep forgetting to do so? Alternatively, how do I configure the project so that it will work while I debug it on IIS with the wwwroot base path?
You need to add a build task to your CSPROJ Blazor WASM.
Important note: you need Newtonsoft 13.0.1, System.Text.Json isn't supported.
<UsingTask TaskName="ReplaceBaseHRef" TaskFactory="RoslynCodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
<ParameterGroup>
<InputFilename ParameterType="System.String" Required="true" />
<AppSettingsfile ParameterType="System.String" Required="true" />
<BaseHRefAttribute ParameterType="System.String" Required="true" />
</ParameterGroup>
<Task>
<Reference Include="$(NugetPackageRoot)\newtonsoft.json\13.0.1\lib\netstandard2.0\Newtonsoft.Json.dll" />
<Using Namespace="System" />
<Using Namespace="System.IO" />
<Using Namespace="System.Text" />
<Using Namespace="System.Text.RegularExpressions" />
<Using Namespace="Newtonsoft.Json" />
<Using Namespace="Newtonsoft.Json.Linq" />
<Code Type="Fragment" Language="C#">
<![CDATA[
var inputFile = File.ReadAllText(InputFilename);
var appsetting = File.ReadAllText(AppSettingsfile);
JObject appsettings = JObject.Parse(appsetting);
var baseHRef = appsettings[BaseHRefAttribute].Value<string>();
if (!string.IsNullOrEmpty(baseHRef)) {
Log.LogMessage( MessageImportance.High, baseHRef );
var outputFile = InputFilename;
var matchExpression = "\\<base\\ href=\\\"(.*)\\\"\\ \\/\\>";
var newBaseHRef = $"<base href=\"{baseHRef}\" />";
File.WriteAllText(
outputFile,
Regex.Replace(inputFile, matchExpression, newBaseHRef)
);
}
]]>
</Code>
</Task>
</UsingTask>
<Target Name="ReplaceBaseHRef" AfterTargets="Publish">
<ReplaceBaseHRef InputFilename="$(PublishDir)wwwroot\index.html" AppSettingsfile="$(PublishDir)wwwroot\appsettings.Production.json" BaseHRefAttribute="BaseHRef" />
</Target>
Now take your appsettings.Production.json and add the configuration setting as:
{
"BaseHRef": "/client/",
...
}
now launch the Publish (in folder, IIS or on Azure).
Your index.html will contain <base href="/client/" />.

MSBuild: How to access a property value set by a Target during the Post Build event in Visual Studio

I have a PostBuild event which invokes a batch file and I need to pass in a particular parameter into the batch file. This parameter is populated through another task which is invoked through a Target configured to run before the PostBuildEvent.
I can see that it gets successfully displayed when displayed using the element as part of the section.
But $(TargetFrameworkToolsFolderPath) under the PostBuildEvent has an "" value. Is there a way to access this custom property in the post build event?
Example:
<UsingTask TaskName="GetTargetFrameworkToolsFolderName" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
<ParameterGroup>
<SDKFolderPath ParameterType="System.String" Required="true" />
<TargetFrameworkVersionStr ParameterType="System.String" Required="true" />
<TargetFrameworkToolsFolder ParameterType="System.String" Output="true" />
</ParameterGroup>
<Task>
<Code Type="Fragment" Language="cs">
TargetFrameworkToolsFolder = SDKFolderPath + "\\" + "bin\\NETFX " + TargetFrameworkVersionStr.Substring(1) + " Tools\\";
</Code>
</Task>
</UsingTask>
<Target Name="FindTargetFrameworkToolsFolderPath" BeforeTargets="PostBuildEvent">
<GetFrameworkSdkPath>
<Output TaskParameter="Path" PropertyName="SdkPath" />
</GetFrameworkSdkPath>
<GetTargetFrameworkToolsFolderName SDKFolderPath="$(SdkPath)" TargetFrameworkVersionStr="$(TargetFrameworkVersion)">
<Output PropertyName="TargetFrameworkToolsFolderPath" TaskParameter="TargetFrameworkToolsFolder"/>
</GetTargetFrameworkToolsFolderName>
<Message Text="$(TargetFrameworkToolsFolderPath)" Importance="normal" /> --> Displayed correctly here
</Target>
<PropertyGroup>
<PostBuildEvent>
call $(ProjectDir)AfterBuildCommands.bat $(TargetFrameworkToolsFolderPath) --> The TargetFrameworkToolsFolderPath property value here seems to be empty.
</PostBuildEvent>
</PropertyGroup>
But $(TargetFrameworkToolsFolderPath) under the PostBuildEvent has an
"" value. Is there a way to access this custom property in the post
build event?
In fact, <PostBuildEvent> is a property and MSBuild reads all the properties first and then executes all targets.
If you put these below outside the target which defines the property TargetFrameworkToolsFolderPath, these below will always execute first, as expected, the values of TargetFrameworkToolsFolderPath will be empty.
To avoid it, you should put the PostBuildEvent and TargetFrameworkToolsFolderPath properties in the same target and make sure the target is executed early enough, such as, run after PrepareForBuild target.
<PropertyGroup>
<PostBuildEvent>
call $(ProjectDir)AfterBuildCommands.bat $(TargetFrameworkToolsFolderPath) --> The TargetFrameworkToolsFolderPath property value here seems to be empty.
</PostBuildEvent>
</PropertyGroup>
Solution
Try this below:
<UsingTask TaskName="GetTargetFrameworkToolsFolderName" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.Core.dll">
<ParameterGroup>
<SDKFolderPath ParameterType="System.String" Required="true" />
<TargetFrameworkVersionStr ParameterType="System.String" Required="true" />
<TargetFrameworkToolsFolder ParameterType="System.String" Output="true" />
</ParameterGroup>
<Task>
<Code Type="Fragment" Language="cs">
TargetFrameworkToolsFolder = SDKFolderPath + "\\" + "bin\\NETFX " + TargetFrameworkVersionStr.Substring(1) + " Tools\\";
</Code>
</Task>
</UsingTask>
<Target Name="MyFindTargetFrameworkToolsFolderPath" AfterTargets="PrepareForBuild">
<GetFrameworkSdkPath>
<Output TaskParameter="Path" PropertyName="SdkPath" />
</GetFrameworkSdkPath>
<GetTargetFrameworkToolsFolderName SDKFolderPath="$(SdkPath)" TargetFrameworkVersionStr="$(TargetFrameworkVersion)">
<Output PropertyName="TargetFrameworkToolsFolderPath" TaskParameter="TargetFrameworkToolsFolder" />
</GetTargetFrameworkToolsFolderName>
<PropertyGroup>
<PostBuildEvent> call $(ProjectDir)AfterBuildCommands.bat $(TargetFrameworkToolsFolderPath) --> The TargetFrameworkToolsFolderPath property value here seems to be empty.</PostBuildEvent>
</PropertyGroup>
<Message Text="$(TargetFrameworkToolsFolderPath)" Importance="normal" />
</Target>
Hope it could help you.

How to get property value of a project file using msbuild

Using MSBuild I include a solution>
<ItemGroup>
<ProjectToBuild Include="$(SVNLocalPath)\$(SolutionName)"> </ProjectToBuild>
</ItemGroup>
I need to include all the *.csproj file from the solution with the condition of the proj file contain or define a property; for example if x.csproj contain a defined property "TestProjectType" would like to include the project into my itemGroup
something like this
<Target Name = "TestProperties">
<Message Text="TestProperties"/>
<ItemGroup>
<AllProj Include="$(SVNLocalPath)\*.csproj"/>
<AllTestProj Include="%(AllProj.Identity)" Condition="%(AllProj.ProjectTypeGuids)=={3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}"/>
</ItemGroup>
<Message Text="#(AllTestProj )"/>
</Target>
Thanks
You can achieve that through custom task.
A simple sample to check a property (test) in all projects exclude current project of current solution:
<UsingTask TaskName="GetPropertyTask" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v12.0.dll">
<ParameterGroup>
<ProjectFile ParameterType="System.String" Required="true" />
<BuildOutput ParameterType="System.String[]" Output="true" />
</ParameterGroup>
<Task>
<Reference Include="System.Xml"/>
<Reference Include="Microsoft.Build"/>
<Using Namespace="Microsoft.Build" />
<Using Namespace="Microsoft.Build.Evaluation" />
<Using Namespace="Microsoft.Build.Utilities" />
<Code Type="Fragment" Language="cs">
<![CDATA[
var properties = new Dictionary<string, string>
{
{ "Configuration", "$(Configuration)" },
{ "Platform", "$(Platform)" }
};
//Log.LogMessage(MessageImportance.High, "customLog");
// Log.LogMessage(MessageImportance.High, ProjectFile);
var collection = new ProjectCollection(properties);
var project = collection.LoadProject(ProjectFile);
ProjectProperty pp = project.Properties.Where(p => p.Name == "MyCustomProperty").FirstOrDefault();
string customValue = pp==null?"empty":pp.EvaluatedValue;
BuildOutput = new String[] { customValue };
]]></Code>
</Task>
</UsingTask>
<Target Name="AfterBuild">
<GetPropertyTask ProjectFile="%(ProjectToScan.FullPath)">
<Output ItemName="ProjectToScanOutput" TaskParameter="BuildOutput"/>
</GetPropertyTask>
<Message Text="ClassLibrary1" Importance="high" Condition="'%(ProjectToScanOutput.Identity)' == 'test'" />
</Target>
More information, please refer to this article.

XPath expression for parsing WiX Processing-Instructions as MsBuild properties

I have the following wix include VersionFile.wxi
<?xml version="1.0" encoding="utf-8"?>
<Include>
<?define ProductVersionMajor = "1" ?>
<?define ProductVersionMinor = "00" ?>
<?define ProductName= "MyProduct" ?>
<?define UpgradeCode = "myUpgradeCode" ?>
</Include>
Now I want to get e.g. the ProductVersionMajor as "1" or ProductName "MyProduct" (without quotes) using XmlPeek and a XPath query. With following code
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="Test">
<XmlPeek XmlInputPath="VersionFile.wxi"
Query="//processing-instruction('define')[starts-with(., "ProductVersionMajor =")]">
<Output TaskParameter="Result" ItemName="Peeked" />
</XmlPeek>
<XmlPeek XmlInputPath="VersionFile.wxi"
Query="//processing-instruction('define')[starts-with(., "ProductVersionMajor=")]">
<Output TaskParameter="Result" ItemName="Peeked" />
</XmlPeek>
<Message Text="#(Peeked)"/>
</Target>
</Project>
I already got it down to
<?define ProductVersionMajor = "1" ?>
But goal would be
1
Any help how to tweak the XPath query highly appreciated. Also it'll be great to have a placeholder "ProductVersionMajor*=" instead using XmlPeek twice.
<XmlPeek XmlInputPath="ProductVersion.wxi"
Query="substring-before(substring-after(//processing-instruction("define")[starts-with(., "ProductVersionMajor=")],"),")">
<Output TaskParameter="Result" ItemName="Peeked" />
</XmlPeek>
unfortunately only produces an
error MSB3734: XPath Query "substring-before(substring-after(//processing-instruction("define")[starts-with(., "ProductVersionMajor=")],"),")" cannot be loaded. 'substring-before(substring-after(//processing-instruction("define")[starts-with(., "ProductVersionMajor=")],"),")' has an invalid token.
Assume that XmlPeek needs some more custom XPath syntax possibly?
Yes. Tried it as well. Now also tried
Query="substring-before(substring-after(//processing-instruction('define')[starts-with(., 'ProductVersionMajor =')],&apos;"&apos;),&apos;"&apos;) ">
Also no success. Error is
error MSB4018: The "XmlPeek" task failed unexpectedly.\r
error MSB4018: System.Xml.XPath.XPathException: Expression must evaluate to a node-set.\r
error MSB4018: at System.Xml.XPath.XPathNavigator.Select(XPathExpression expr)\r
error MSB4018: at Microsoft.Build.Tasks.XmlPeek.Execute()\r
error MSB4018: at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()\r
error MSB4018: at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask(ITaskExecutionHost taskExecutionHost, Task
LoggingContext taskLoggingContext, TaskHost taskHost, ItemBucket bucket, TaskExecutionMode howToExecuteTask, Boolean& taskResult)
Form the xpath point of view the following should do:
Query='substring-before(
substring-after(
//processing-instruction("define")[starts-with(., "ProductVersionMajor =")]
,
&apos;"&apos;
)
,
&apos;"&apos;
)'
I solved this problem with this approach. Hope it helps:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<UsingTask TaskName="GetWixDefine"
TaskFactory="CodeTaskFactory"
AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
<ParameterGroup>
<File ParameterType="System.String" Required="true" Output="false" />
<DefineName ParameterType="System.String" Required="true" Output="false" />
<Value ParameterType="System.String" Output="true" />
</ParameterGroup>
<Task>
<Reference Include="System.Xml" />
<Using Namespace="System" />
<Using Namespace="System.Text.RegularExpressions" />
<Using Namespace="System.Xml" />
<Code Type="Fragment" Language="cs">
<![CDATA[
this.Value = string.Empty;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(this.File);
string selector = string.Format("//processing-instruction('define')[starts-with(., '{0}')]", this.DefineName);
XmlNode defineNode = xmlDoc.SelectSingleNode(selector);
if (defineNode == null)
throw new Exception("define not found");
string regex = string.Format("{0}[ ]*=[ ]*\"(?<value>.*)\"", DefineName);
Match match = Regex.Match(defineNode.InnerText, regex);
if (!match.Success)
throw new Exception("cannot match correctly");
this.Value = match.Groups["value"].Value;
]]>
</Code>
</Task>
</UsingTask>
<Target Name="BeforeBuild">
<GetWixDefine File="VersionFile.wxi" DefineName="ProductVersion">
<Output TaskParameter="Value" ItemName="ProductVersionValue"/>
</GetWixDefine>
<Message Importance="High" Text="ProductVersion: #(ProductVersionValue)"/>
</Target>
</Project>

Spring-Ibatis Deployement exception

Exception:
Caused by: org.springframework.core.NestedIOException: Failed to parse config resource: ServletContext resource [/WEB-INF/SqlMapConfig.xml]; nested exception is com.ibatis.common.xml.NodeletException: Error parsing XML. Cause: java.lang.RuntimeException: Error parsing XPath '/sqlMapConfig/sqlMap'. Cause: java.io.IOException: Could not find resource WEB-INF/ADCampaignDetailsSQLMap.xml
SqlMapConfig.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL MAP Config 2.0//EN" "http://www.ibatis.com/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<settings useStatementNamespaces="true"/>
<sqlMap resource="WEB-INF/ADCampaignDetailsSQLMap.xml"/>
</sqlMapConfig>
ADCampaignDetailsSQLMap.xml is placed inside WEB-INF of my project folder
And the Above exception is raised when i copied the war file to webapps folder ..
Can any one give me solution for this?
thanks in advance
Edit:
build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project
name="adblendservice"
default="war" >
<property environment="env" />
<property
name="builddir"
value="build/" />
<property
name="srcdir"
value="src/main/java/" />
<property
name="deploydir"
value="deploy/" />
<property
name="wardir"
value="src/main/webapp/" />
<property
name="libdir"
value="${wardir}/WEB-INF/lib/" />
<property file="build.properties" />
<path id="project-classpath" >
<fileset
dir="web/WEB-INF/lib"
includes="*.jar" />
<fileset
dir="${tomcat-home}/lib"
includes="*.jar" />
<!--
<fileset dir="${tomcat-home}/common/lib" includes="*.jar" />
<fileset dir="${tomcat-home}/server/lib" includes="*.jar" />
-->
</path>
<target name="clean" >
<delete
dir="${builddir}"
failonerror="true" />
<echo message="Creating build directories" />
</target>
<target name="war" >
<mkdir dir="${builddir}" />
<mkdir dir="${builddir}/adblendservice/WEB-INF/classes" />
<mkdir dir="${deploydir}" />
<path id="basepath" >
<fileset dir="${wardir}/WEB-INF/lib" >
<include name="**/*.jar" />
</fileset>
</path>
<javac
destdir="${builddir}/adblendservice/WEB-INF/classes"
includeantruntime="false"
srcdir="${srcdir}" >
<classpath refid="basepath" />
</javac>
<war
update="update"
warfile="${builddir}/adblendservice.war"
webxml="${wardir}/WEB-INF/web.xml" >
<classes dir="${builddir}/adblendservice/WEB-INF/classes" />
<fileset dir="${srcdir}" >
<include name="**/*.xml" />
</fileset>
<lib dir="${wardir}/WEB-INF/lib" />
<fileset dir="${wardir}" >
<include name="**/*.xml" />
</fileset>
</war>
</target>
<target
name="deploy"
depends="clean, war" >
<copy
file="${builddir}/adblendservice.war"
todir="${deploydir}" >
</copy>
</target>
</project>
The root of the classpath where iBatis searches for xml files is WEB-INF/classes, not the root of the public web site.
Try to move your xml into the classes directory and point to it without path.
Move your XML files to class path or if it is outside class path, then specify path like <sqlMap resource="../WEB-INF/ADCampaignDetailsSQLMap.xml"/>

Resources