Skip to content

Commit

Permalink
add integration test
Browse files Browse the repository at this point in the history
start documentation
  • Loading branch information
jschwarz-eitco-de committed Jun 26, 2024
1 parent bccaf76 commit 7faf8f0
Show file tree
Hide file tree
Showing 29 changed files with 676 additions and 33 deletions.
29 changes: 2 additions & 27 deletions .mvn/maven-git-versioning-extension.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,12 @@
<properties>
<version.original>${version}</version.original>
<version.numeric>${ref.tag}</version.numeric>
<version.symbolic>latest</version.symbolic>
<version.symbolic>${property.lts.name:-latest}</version.symbolic>
<version.semantic>${ref.tag}</version.semantic>
<scm.branch>${ref.slug}</scm.branch>
<version.unique>${ref.tag}</version.unique>
</properties>
</ref>
<ref type="branch">
<pattern><![CDATA[lts-(?<lts>(.*))-development]]></pattern>
<version>lts-${ref.lts}-development-SNAPSHOT</version>
<properties>
<version.original>${version}</version.original>
<version.numeric>${version.release}</version.numeric>
<version.symbolic>${ref.lts}-development-SNAPSHOT</version.symbolic>
<version.semantic>${version.release}-${ref.lts}-development-SNAPSHOT</version.semantic>
<scm.branch>${ref.slug}</scm.branch>
<version.unique>${version.release}-${ref.lts}-development-build.${build.number}</version.unique>
</properties>
</ref>
<ref type="branch">
<pattern>main</pattern>
<version>${version}</version>
Expand All @@ -39,18 +27,6 @@
<version.unique>${version}-build.${build.number}</version.unique>
</properties>
</ref>
<ref type="branch">
<pattern><![CDATA[lts-(?<ltsName>(.*))]]></pattern>
<version>${version}</version>
<properties>
<version.original>${version}</version.original>
<version.numeric>${version.release}</version.numeric>
<version.symbolic>${ref.ltsName}-SNAPSHOT</version.symbolic>
<version.semantic>${version}</version.semantic>
<scm.branch>${ref.slug}</scm.branch>
<version.unique>${version.release}-build.${build.number}</version.unique>
</properties>
</ref>
<ref type="branch">
<pattern><![CDATA[((feature|bugfix)/)?(?<feature>[a-zA-Z][a-zA-Z][a-zA-Z]*-[0-9]+).*]]></pattern>
<version>${ref.feature}-SNAPSHOT</version>
Expand Down Expand Up @@ -111,5 +87,4 @@
<version.unique>${version.release}-${commit}-build.${build.number}</version.unique>
</properties>
</rev>

</configuration>
</configuration>
142 changes: 140 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,144 @@
[![Build status](https://img.shields.io/github/actions/workflow/status/eitco/dotnet-maven-plugin/deploy.yaml?branch=main&style=for-the-badge&logo=github)](https://github.com/eitco/dotnet-maven-plugin/actions/workflows/deploy.yaml)
[![Maven Central Version](https://img.shields.io/maven-central/v/de.eitco.cicd/dotnet-maven-plugin?style=for-the-badge&logo=apachemaven)](https://central.sonatype.com/artifact/de.eitco.cicd/dotnet-maven-plugin)

# dotnet-maven-plugin
# dotnet maven plugin

a maven plugin providing a build lifecycle for dotnet
This maven plugin provides a build lifecycle for dotnet. It is intended fpr situations where you need to build
one or more dotnet binaries inside a maven build. Such situations arise if you have a java application that does contain
some code that ports it to .Net.

# usage

To activate this build lifecycle add this plugin to your pom and set `packaging` to `nuget`:

````xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>my.group.id</groupId>
<artifactId>my-artifact-id</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>nuget</packaging>

<build>
<plugins>
<plugin>
<groupId>de.eitco.cicd</groupId>
<artifactId>dotnet-maven-plugin</artifactId>
<version>0.0.3</version>
<extensions>true</extensions> <!-- (1) -->
</plugin>
</plugins>
</build>

</project>
````
> 📘 you need to activate extensions for this plugin (1)
When maven is started this will activate a build lifecycle that calls the dotnet executable to build the current project.
This assumes that a valid sln or csproj file is in the same path as the pom and that dotnet is available in the PATH
environment variable.

# build lifecycle

The following goals are bound to the build lifecycle:

## initialize

The `initialize` goal is bound to the `initialize` phase. This goal registers nuget source repositories. A special one
that helps mimic mavens local repository and additionally all repositories that are configured. For example:

````xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

...
<build>
<plugins>
<plugin>
<groupId>de.eitco.cicd</groupId>
<artifactId>dotnet-maven-plugin</artifactId>
<version>0.0.3</version>
<extensions>true</extensions>
<configuration>
<nugetSources>
<organization-repository-id>https://repo1.organization.org/nuget</organization-repository-id>
<project-repository-id>https:///epo1.organization.org/project-nuget</project-repository-id>
</nugetSources>
</configuration>
</plugin>
</plugins>
</build>

</project>
````

This will add two custom nuget sources one named `organization-repository-id` that is assumed to be located at
`https://repo1.organization.org/nuget` and another one named `project-repository-id` that is assumed to be located at
`https://repo1.organization.org/project-nuget`.

> 📘 Should you have registered any `<service>` elements in your settings.xml having the source name as `id`, those
> credentials will be added to the corresponding nuget source.
> ⚠️ On non-windows platforms dotnet/nuget is unable to store credentials encrypted. On these platforms the credentials
> will be stored in plain text
Additionally, a source `maven-nuget-local` pointing to the local directory `~/.m2/repository` will be added. Should any
of these sourced already be added, they will be updated with their current config.

### The local nuget repository

The `initialize` goal also creates a local directory as nuget source, where a later goal will 'install' its artifacts to.
This enables a maven builds, where two different dotnet modules of the same build reactor have a dependency between them.

By default, the name of this source is `maven-nuget-local` and its location is `${settings.localRepository}`. Those
can be overridden with the `<localMavenNugetRepositoryName>` and `<localMavenNugetRepositoryBaseDirectory>` parameters, respectively.

## build

The `build` goal is bound to the `compile` phase. It will call `dotnet build` on the current project. Be that a .sln
or .csproj file. It will always add the command line option `-p:Version=<projectVersion>` with `projectVersion` being
the goals parameter of the same name. This way the version of the build artifacts are managed in the pom.

## test

The `test` goal is bound to the `test` phase. It will call `dotnet test` to execute test. It will add the command line
parameter `--no-build`, since the `compile` phase will already be called before the `test` phase. It will always configure
the `trx` logger and transform the results to a valid `junit` description - enabling ci servers to collect the tests
results in the default format for maven builds. The goal will honour the reactors failure behaviour. Also, it honours the
property `skipTests`, skipping its execution when set to true.

## pack

The `pack` goal is bound to the `package` phase. It calls `dotnet pack` creating nuget packages (*.nupgk). The goal will
call dotnet with `--no-build` as the project was already built in the `compile` phase. As the `build` goal it will set
the dotnet `Version` property to the goals `projectVersion` parameter, managing the version in the pom. Also, it will set
the `--output` command line argument to the goals `targetDirectory` parameter, which defaults to `${project.build.directory}/dotnet`
thus being deleted by a `mvn clean` command.

## install

The `install` goal is bound to the `install` phase. It does not call `dotnet` at all. Since the `package` phase was
already executed, it assumes the builds delivery artifacts to be located in the directory denoted by the goals
`targetDirectory` parameter. It will simply copy all *.nupkg files located there to directory configured to be the
[local nuget directory](#the-local-nuget-repository)

## push

The `push` goal is bound to the `deploy` phase. It calls `nuget push` on every nupgk file located in the configured
`targetDirectory`.

# clean lifecycle

This plugin also provides a `clean` lifecycle. It is pretty simple: Additionally to the maven default `clean` goal, a
custom `clean` goal is called, which simply calls `dotnet clean`

# examples

Examples can be found with the [integration tests](src/it)
3 changes: 3 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@
<plugin>
<artifactId>maven-resources-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-invoker-plugin</artifactId>
</plugin>
</plugins>
</build>

Expand Down
4 changes: 4 additions & 0 deletions src/it/csproj/A.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace de.eitco.cicd.test;

public class A {
}
15 changes: 15 additions & 0 deletions src/it/csproj/dotnet-csproj.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project>
<PropertyGroup>
<OutputType>library</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<BaseOutputPath>target\bin</BaseOutputPath>
<BaseIntermediateOutputPath>target\obj</BaseIntermediateOutputPath>
<OutputPath>target\obj</OutputPath>
<MSBUildProjectExtensionsPath>target\obj</MSBUildProjectExtensionsPath>
</PropertyGroup>

<Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk"/>
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk"/>
</Project>
27 changes: 27 additions & 0 deletions src/it/csproj/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>de.eitco.cicd.text</groupId>
<artifactId>dotnet-test-csproj</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>nuget</packaging>

<build>
<plugins>
<plugin>
<groupId>de.eitco.cicd</groupId>
<artifactId>dotnet-maven-plugin</artifactId>
<version>@project.version@</version>
<extensions>true</extensions>
<configuration>
<assemblyVersion>0.0.1</assemblyVersion>
<configurationName>Release</configurationName>
</configuration>
</plugin>
</plugins>
</build>

</project>
40 changes: 40 additions & 0 deletions src/it/modules/dependency/dependency.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{4B20F6E5-6534-438B-9FC6-8085C3E0EFA1}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "main", "main", "{12E0FD4A-9808-47D0-994D-1595C74DC0F9}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "dependency", "src\main\cs\dependency.csproj", "{C51B1D9F-2E38-4647-81A5-7E63AE545717}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{50621265-6679-400B-83D1-177D14244CC2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "dependency-test", "src\test\cs\dependency-test.csproj", "{71A97C3F-E252-4AF4-BB52-5050899FC2A3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C51B1D9F-2E38-4647-81A5-7E63AE545717}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C51B1D9F-2E38-4647-81A5-7E63AE545717}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C51B1D9F-2E38-4647-81A5-7E63AE545717}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C51B1D9F-2E38-4647-81A5-7E63AE545717}.Release|Any CPU.Build.0 = Release|Any CPU
{71A97C3F-E252-4AF4-BB52-5050899FC2A3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{71A97C3F-E252-4AF4-BB52-5050899FC2A3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{71A97C3F-E252-4AF4-BB52-5050899FC2A3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{71A97C3F-E252-4AF4-BB52-5050899FC2A3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{12E0FD4A-9808-47D0-994D-1595C74DC0F9} = {4B20F6E5-6534-438B-9FC6-8085C3E0EFA1}
{C51B1D9F-2E38-4647-81A5-7E63AE545717} = {12E0FD4A-9808-47D0-994D-1595C74DC0F9}
{50621265-6679-400B-83D1-177D14244CC2} = {4B20F6E5-6534-438B-9FC6-8085C3E0EFA1}
{71A97C3F-E252-4AF4-BB52-5050899FC2A3} = {50621265-6679-400B-83D1-177D14244CC2}
EndGlobalSection
EndGlobal
25 changes: 25 additions & 0 deletions src/it/modules/dependency/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>de.eitco.cicd.text</groupId>
<artifactId>dotnet-test-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>

<artifactId>dotnet-test-modules-dependency</artifactId>
<packaging>nuget</packaging>

<build>
<plugins>
<plugin>
<groupId>de.eitco.cicd</groupId>
<artifactId>dotnet-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
8 changes: 8 additions & 0 deletions src/it/modules/dependency/src/main/cs/A.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace de.eitco.cicd.test;

public class A : IA {

public void Noop() {
}

}
6 changes: 6 additions & 0 deletions src/it/modules/dependency/src/main/cs/IA.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace de.eitco.cicd.test;

public interface IA {

public void Noop();
}
15 changes: 15 additions & 0 deletions src/it/modules/dependency/src/main/cs/dependency.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project>
<PropertyGroup>
<OutputType>library</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<BaseOutputPath>..\..\..\target\bin</BaseOutputPath>
<BaseIntermediateOutputPath>..\..\..\target\obj</BaseIntermediateOutputPath>
<OutputPath>..\..\..\target\obj</OutputPath>
<MSBUildProjectExtensionsPath>..\..\..\target\obj</MSBUildProjectExtensionsPath>
</PropertyGroup>

<Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk"/>
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk"/>
</Project>
12 changes: 12 additions & 0 deletions src/it/modules/dependency/src/test/cs/ATest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace de.eitco.cicd.test;

public class ATest {


public void test() {

A a = new A();
a.Noop();
}

}
Loading

0 comments on commit 7faf8f0

Please sign in to comment.