-1

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) ?

fharreau
  • 2,105
  • 1
  • 23
  • 46
  • There is `DownloadFile` task and `Unzip` task ... *Did I failed my search...* hehehe ... I just put "download file msbuid" into internet search and find ... guess what ... official documentation of msbuild with ... drums ... examples – Selvin Mar 27 '23 at 12:56
  • If I have to build my own I will use these tasks, but maybe someone already did all the plumbing around. My question is not just about downloading some random file but integrating within the whole Maven system. – fharreau Mar 27 '23 at 13:01
  • note: from `DownloadFile` task use output parameter `DownloadedFile` (you can rename it) as `SourceFiles` in `Unzip` task (like `$(DownloadedFile)`)... you need also point that Unzip task depends on DownloadFile task – Selvin Mar 27 '23 at 13:03
  • you didn't wrote about *integrating within the whole Maven system* in the question – Selvin Mar 27 '23 at 13:06
  • I am looking for a way to maven artifacts (from the csproj or another file) during build. Not how to do it. I removed the part where I talk about a way to do it to avoid confusion. – fharreau Mar 27 '23 at 13:15
  • artifact is `jar` (so it's zip) ... download, unzip, do the custom stuff with the unziped xsd ... maven is irrelevant here ... unless you wana parse `pom.xml` then just run maven from msbuild (as external program) ... you can parse xml in msbuild but this solution would be very project specific – Selvin Mar 27 '23 at 13:18
  • You could write in the question what you wana achive: how artifact is defined(you have link to jar or just aaaa:aaaa:xxxx) ? what artifact contains ... how you wana use it ... also you could change title ... since solution for *Download maven artifacts during msbuild* is pretty simple – Selvin Mar 27 '23 at 13:21
  • Yeah, unless someone already did it, in a way that repects maven formalism, managing private repo url for exemple, or anything I didn't think about because I am no maven expert. Of course I will do it myself if it does not already exist but I prefer not to reinvent the wheel. – fharreau Mar 27 '23 at 13:22
  • 1
    https://gist.github.com/SelvinPL/f082f4507b39fdf132fda00ae9bd9ff8 – Selvin Mar 27 '23 at 15:44
  • Yes, I was looking for that kind of stuff even if I wasn't asking for someone to implement it. Nevertheless, thank you. You can post it as answer ! – fharreau Mar 29 '23 at 07:24
  • Remember that it will not download dependencies – Selvin Mar 29 '23 at 07:29

1 Answers1

0

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>
fharreau
  • 2,105
  • 1
  • 23
  • 46