Apache Ivy & Settings File - apache

I'm writing my first Ivy configuration for a new Java project and am trying to hook my ivy-settings.xml file up to my buildscript.
I've followed all the tutorials and have correctly added the xmlns:ivy="antlib:org.apache.ivy.ant" namespace to my build.xml file. So far I've tried running my resolve and cleancache targets, which run ivy:resolve and ivy:cleancache respectively, and all seems to be working (Ant can find Ivy).
However...when I run ivy:resolve, it defaults to go right to the public repo. Since my resolvers are written to look in my SVN root, I have to conclude that Ivy does not see my ivy-settings.xml file and is going to the public repos by default.
I am purposely keeping my ivy-settings.xml file as a separate project in source control because it will be used by all my projects.
So my question:
How do I instruct the Ant buildscipt to look for a checked-out version of ivy-settings.xml somewhere else in my file system, not just sitting there locally in the same directory as build.xml?

Change the settings file name to:
ivysettings.xml
If you want to change this default name and location (same directory as build file) use the "settings" task
Update
This is how I ensure that the settings file is loaded before invoking the ivy resolve of cleancache tasks. Create a target called init that is declared as a dependency
<target name="init">
<ivy:settings file="../../ivysettings.xml"/>
</target>
<target name="resolve" depends="init">
<ivy:resolve/>
</target>
<target name="clean" description="Cleanup build directory">
<delete dir="${build.dir}"/>
</target>
<target name="clean-all" depends="init,clean" description="Clean and purge caches">
<!-- Purge the ivy cache -->
<ivy:cleancache/>
</target>

Related

Is it possible to extract the value of element in csproj file with dotnet CLI?

csproj file contains project properties which I'd like to use in CI/CD pipeline.
Is it possible to extract those values with dotnet cli (or some other standard tool) without parsing xml via some standalone script?
For example having the project:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Version>1.0.0</Version>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>
</<Project>
I need to resolve the value of <Version>.
It's also wroth noting that project may contain conditional elements, and ideally it would be nice to resolve properties in the context of predefined variables.
MSBuild is very extensible. You could use a target to write the version number to a file:
<Target Name="WriteVersion" AfterTargets="Build">
<WriteLinesToFile Lines="$(Version)"
File="$(IntermediateOutputPath)version.txt" />
</Target>
That would write a version.txt file to a folder such as obj/Debug/net5.0 (depending on the Configuration and TargetFramework).
Also be sure to check out https://msbuildlog.com/ for how to investigate / debug builds.

MSBuild nuget RestoreOutputPath how to make it work?

New msbuild csproj format have got integrated nuget commands. It's possible to change default path where project assets will be restored by using <RestoreOutputPath>obj\profile7</RestoreOutputPath> command in project file.
But if I add <RestoreOutputPath>obj\profile7</RestoreOutputPath> to csproj file consequent commands
dotnet restore myproj.sln
dotnet build myproj.sln
produce build errors
obj\project.assets.json' not found. Run a NuGet package restore to generate this file.
How to tell MSBuild to get nuget assets from this obj\Profile7 path during the build command?
The restore output path needs to be the same as MSBuildProjectExtensionsPath so that the nuget generated props and targets files will be imported by the common props and targets. as well as BaseIntermediateOutputPath will be the default for composing the path to ProjectAssetsFile.
At least for the NuGet imports, it is important that MSBuildProjectExtensionsPath or BaseIntermediateOutputPath is set before the SDK props file is imported.
The simplest way to solve all of these issues is to set BaseIntermediateOutputPath very early in the project so that all components will take its value as a default base path - this is essentially redirecting obj to somewhere else.
This conflicts with the <Project SDK="..."> syntax since there is no way to set properties before the SDK's props file. To work around this, the project can be changed like this:
<Project>
<!-- This needs to be set before Sdk.props -->
<PropertyGroup>
<BaseIntermediateOutputPath>obj\SomeSubDir\</BaseIntermediateOutputPath>
</PropertyGroup>
<Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk" />
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.1</TargetFramework>
</PropertyGroup>
<!-- other content -->
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />
</Project>
An alternative would be to create a Directory.Build.props file that will be automatically imported early enough, but this would apply the value to all projects in the directory and take away the ability to specify the value per project.

web deploy copy a file from drop location to project

I have a console project in my solution.
Now I want that exe of that project should be added to my main project post deployment using web deploy.
How can I achieve that?
Regards,
Gautam
There are two high level solutions for this:
Option 1: Copy the file into App_Data
You can copy the exe into the App_Data folder as part of a post build event or as part of the script below. It's your choice.
Now that it's there, we have another problem. The WPP only includes files that are part of the project when it deploys. To get around this, you can create a WebProjectName.wpp.targets file to the root of the web application with the following contents:
<Project>
<PropertyGroup>
<BeforeAddContentPathToSourceManifest>
$(BeforeAddContentPathToSourceManifest);
IncludeExeInDeployment;
</BeforeAddContentPathToSourceManifest>
</PropertyGroup>
<Target Name="IncludeExeInDeployment">
<Copy SourceFiles="$(WebPublishPipelineProjectDirectory)\App_Data\Console\*"
TargetFolder="$(WPPAllFilesInSingleFolder)\App_Data\Console" />
</Target>
</Project>
(You could just as easily skip the interim step and copy the exe from it's original home into the $(WPPAllFilesInSingleFolder) folder)
Option 2: Include the exe as a separate provider
This one requires a bit more understanding of msdeploy, but gives you the option to deploy the exe wherever you want on the target server.
Basically it involves adding an additional dirPath provider in the deployment. Again, add a wpp.targets file in the root:
<Project>
<PropertyGroup>
<AfterAddContentPathToSourceManifest>
$(AfterAddContentPathToSourceManifest);
IncludeConsoleAppInDeployment;
</AfterAddContentPathToSourceManifest>
</PropertyGroup>
<Target Name="IncludeConsoleAppInDeployment">
<ItemGroup>
<MsDeploySourceManifest Include="dirPath">
<Path>full path to console directory</Path>
</MsDeploySourceManifest>
</ItemGroup>
</Target>
</Project>
You'll also need to replace the path in your pubxml to specify where the exe will go on the far end:
<ItemGroup>
<MsDeploySetParameters Include="ConsoleAppPath">
<Kind>ProviderPath</Kind>
<Scope>dirPath</Scope>
<Match>regex that matches console directory</Match>
<Value>Path to console application on remote server</Value>
</MsDeploySetParameters>
</ItemGroup>

Creating MSBuild target hooks

Can someone please point me to a reference about target hooks in MSBuild?
I'm looking for something that will let me define targets to run before and after a specified target. I know this can be done using the DependsOnTargets property but I've seen references to using target hooks and I'd like to explore that area.
Thanks,
Zain
A good list of built-in overridable build process hooks can be found here. For custom targets, the only thing I can think of is to use either the DependsOnTarget attribute (like you mentioned) or the BeforeTargets/AfterTargets attribute (like #Ritch Melton mentioned.) Be careful, the BeforeTargets/AfterTargets are only available in MSBuild 4.0
If you understand the idea behind DependsOnTargets then open up the Microsoft.Common.targets file in the .Net SDK directory (C:\Windows\Microsoft.NET\Framework\v3.5). That file defines the build process for the MSBuild task and .Net projects created by Visual Studio. Look for tags called BeforeXXXX, and AfterXXXX. BeforeBuild and AfterBuild are referenced in the default.csproj file - Snippet:
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
There are others, like Clean, Rebuild, etc..
Define a Target (or Targets) to execute inside those Target elements, like this (Creates a directory, or list of directories based on the value in the Directories Property:
<Target Name="CreateDir">
<MakeDir Directories="D:\Dogs.txt"/>
</Target>
Then include those Targets in the BeforeXXX Target:
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="BeforeBuild" BeforeTargets="CreateDir">
</Target>
</Project>

ivy simple shared repository

I am trying to compile all sub projects of one big project at my company into many jars with managed dependencies, so that not everybody who works at one project only needs to download the latest jars from a shared repository.
ivy seems to be the solution for our problem, because ivy says that it integrates with ant (out build system) very well. But I cant get through the tutorials, they are all somehow more confusing than helpful.
All I want to achieve for the beginning is to have two small Projects. The first one has one class with one method, the second one is just calling this method. The fist project should compile into a jar that is then downloaded by the second project from the shared repository.
Thanks for your help.
A multi-module project is described in the documentation:
http://ant.apache.org/ivy/history/latest-milestone/tutorial/multiproject.html
and the source code is available in subversion:
http://svn.apache.org/viewvc/ant/ivy/core/trunk/src/example/multi-project/
The simplified summary of how it works:
Wrapper build
Invokes each individual module build in the correct order. If Module A depends on module B, then B will be built first:
<project xmlns:ivy="antlib:org.apache.ivy.ant" name="build-all" default="build">
<!--
==========================================================================
Use the ivy buildlist task to create an ordered list of sub-project builds
==========================================================================
-->
<target name="build-list">
<ivy:buildlist reference="build-path">
<fileset dir="." includes="modules/**/build.xml"/>
</ivy:buildlist>
</target>
<!--
==============================
Invoke targets in sub-projects
==============================
-->
<target name="build" depends="build-list" description="Invoke build target on sub-projects">
<subant target="build" buildpathref="build-path" />
</target>
</project>
For more information see the buildlist documentation.
Module build
Each module will download it's dependencies at the beginning of it's build
<target name="init">
<ivy:settings file="../../ivysettings.xml"/>
<ivy:resolve/>
</target>
At at the end, will publish it's built artifacts:
<target name="publish" depends="build" description="Publish module artifacts to the respository">
<ivy:publish resolver="${publish.resolver}" pubrevision="${publish.revision}" overwrite="true">
<artifacts pattern="${build.dir}/[artifact].[ext]"/>
</ivy:publish>
</target>
Don't forget that for all this to work each module must declare what it depends on and what it publishes
<ivy-module version='2.0'>
<info organisation='com.myorg' module='mymod'/>
<publications>
<artifact name="mymod" type="jar"/>
</publications>
<dependencies>
..
</dependencies>
</ivy-module>