terça-feira, 15 de janeiro de 2019

How to Mavenize a Visual Studio project


Since i was so used to work with Maven, due to past Java+Maven experiences, when i was asked to work in C# i felt something was amiss.

Here are my reasons:
  1. The capability of building the solution in the Windows command line (without the need to open Visual Studio IDE)
  2. Being able to build some parts of the project including dependencies
  3. Being able to run test suites during the build
  4. Provide an automated solution that any member of the team should be able to build/test/run with a single command
  5. Integrate the automated solution with Jenkins
I don't know if Microsoft has their own Continuous Integration server/service, but i managed to come up with a pretty elegant solution for what i needed using Open Source/Publicly available software such as Maven/Jenkins, and so, i decided to share.

As usual, i will provide the sources of this project here, so you can download and inspect the sources for yourself, and adapt it to your needs.

Here is a recipe of how to build a new project from the ground up, as well as a showcase of what can be achieved using this set of technologies.

First off, lets create a new Solution/Project. In this case, i've created a WebAPI 2.0 project plus a test project that will be used to test if the WebServices are on-line or not (can also be used to test if the WebServices are working as expected)

This is a pretty standard process, and i will not go into the details on how to create this. I just used the Visual Studio wizards to help me create these 2 projects.

As you can see there is a HealthInfoController and a HealthInfoControllerTest. The first is the WebService we want to test, and the latter is the test. Note that, the test project does not contain any reference to the WebApp project. This means the WebApp.Tests build will not trigger a build of WebApp on the VisualStudio IDE, but it also means that the WebApp.Tests has no access to the WebApp internals, and cannot access the WebApp namespace.

The WebApp.Tests is therefore, an Integration Test project, not a Unit Test project. This means, we have to run WebApp on the IIS Server before starting the test suite.

After you have a buildable solution and tests, we need to add Maven artifacts to the solution, in order to be able to run this as a Maven project.

So, we need a file with instructions on how to start the IIS Server, called iis_express_start.bat. A pre-integration and post-integration scripts that will be used to launch and shutdown the IIS Server running as a child process. And we need a pom.xml and corresponding hierarchy in order to orchestrate this project as an IIS integration test project for Maven.


The Maven artifacts in the root directory include the following:
  • pom.xml - Root description of the Mavenized project
  • module.xml - Skeleton POM descriptor for each of the projects that we want to build and produce binary libraries (DLL's)
  • packageManager.xml - Maven descriptor for NuGet package management resolution
  • test_module.xml - skeleton POM descriptor for unit tests
  • test_module_iis.xml - skeleton POM descriptor for IIS server integration tests
As you can see, all the build + tests can be done from Maven, without the need to start Visual Studio. I can even edit the files using notepad if i'm in a rush.

Here is what a build of this project looks like:







Happy coding!

4 comentários:

  1. Hello, I want to ask something, I can't see which maven command you are running. Would you write it please?

    ResponderEliminar
    Respostas
    1. [INFO] PackageManager ..................................... FAILURE [ 6.423 s]
      [INFO] WebApp ............................................. SKIPPED
      [INFO] WebApp.Tests ....................................... SKIPPED
      [INFO] VSMavenizer ........................................ SKIPPED
      [INFO] ------------------------------------------------------------------------
      [INFO] BUILD FAILURE
      [INFO] ------------------------------------------------------------------------
      [INFO] Total time: 6.680 s
      [INFO] Finished at: 2020-08-15T15:42:52+03:00
      [INFO] Final Memory: 8M/30M
      [INFO] ------------------------------------------------------------------------
      [ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.6.0:exec (i
      nitialize-with-nuget-packages) on project PackageManager: Command execution fail
      ed.: Process exited with an error: 1 (Exit value: 1) -> [Help 1]
      [ERROR]
      [ERROR] To see the full stack trace of the errors, re-run Maven with the -e swit
      ch.
      [ERROR] Re-run Maven using the -X switch to enable full debug logging.
      [ERROR]
      [ERROR] For more information about the errors and possible solutions, please rea
      d the following articles:
      [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionE
      xception


      I tried mvn clean install, mvn clean verify and I got above error. Do you have a solution for this error?

      Eliminar
    2. You are having NuGet issues. Investigate how VS manages NuGet packages nowadays and update the scripts accordingly. Could just be a failure to access the web or a specific server to download the packages.

      Eliminar