Search code examples
c#mavenmsbuildmsbuild-task

Download maven artifacts during msbuild


I am writing a dotnet program that depends on maven artifacts (these artifacts are exposing some XSDs required by my program).

I am looking for a way to reference them in the csproj (an additionnal file in my sources would also be acceptable) so that they will downloaded automatically during the build, as if they were nuget dependencies. But I can't find any existing solution.

Did I failed my search (Ideally I would prefer a solution that does not rely on an another program) ?


Solution

  • Selvin solution (in the question comments) was working but I did not succeed improving it to manage several Maven Repositories neither manage several files to unzip so I end-up with my own package.

    It can surely be improved but it is a start.

    Inspired by Selvin solution:

    • First you need to install the package dotnet add package NMaven
    • Then you can add as many repository as required in your csproj: <MavenRepository Include="Repository" Url="https://path.to.your/repository" />
    • Then you can add as many maven dependencies <MavenReference Include="artifact-id" GroupId="group.id" Version="X.Y.Z.R" />
    • Finally add as many deployment rules as required: <NMavenDeployment Include="DeploymentRule" ArtifactId="artifact-id" Files="path/to/file.ext" Destination="Destination/Folder" />

    Here is the working example shown in the github where we extract the manifest file of a maven dependency and print it in a console.

    <ItemGroup>
        <MavenRepository Include="Mvn-Repo" Url="https://repo1.maven.org/maven2" />
        <MavenReference Include="commons-compress" GroupId="org.apache.commons" Version="1.23.0" />
        <NMavenDeployment Include="MANIFEST.MF" ArtifactId="commons-compress" Files="MANIFEST.MF" Destination="Resources" />
    </ItemGroup>