Skip to content

GoLang multi module project with common code

Igor Maznitsa edited this page Nov 7, 2016 · 3 revisions

If you want to build a multi-application GoLang project sharing some common code between module. (Also you can take a look at the test example included into plugin project)

If you have some problems to build multi-module project then check if you use multi-thread build, if it is enabled then try to build in single-thread mode. There is not direct dependencies recognized by maven in mvn-golang project and order of build in multi-thread mode can be wrong!

Imagine that you want to build two GoLang applications sharing some common code and structure of your project is

|
+-common
|
+-app-1
|
\-app-2

The common project just contains shared code and app-1 and app-2 generate executable files based on common code.

Step 1, Create maven POM project

Just create maven multi-module POM project which will contain similar pom.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>com.igormaznitsa.test.mvngolang</groupId>
    <artifactId>mvngolang-multimodule-example</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>
    
    <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <go.sdk.version>1.7.3</go.sdk.version>
      <mvn.golang.version>2.1.2</mvn.golang.version>
    </properties>

    <modules>
      <module>common</module>
      <module>app-1</module>
      <module>app-2</module>
    </modules>
</project>

In the same folder you should create subfolders common, app-1 and app-2 (as described in the modules section of the main pom.xml)

Step 2, create COMMON

Create pom.xml for the COMMON shared code, we have to disable default build and install actions for the module because it contains just shared code.

<?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>com.igormaznitsa.test.mvngolang</groupId>
    <artifactId>mvngolang-multimodule-example</artifactId>
    <version>1.0-SNAPSHOT</version>
    <relativePath>../pom.xml</relativePath>
  </parent>

  <artifactId>common</artifactId>
  <packaging>mvn-golang</packaging>

  <name>Common</name>

  <build>
    <sourceDirectory>${basedir}${file.separator}src</sourceDirectory>
    <directory>${basedir}${file.separator}bin</directory>
    
    <resources>
      <resource>
        <directory>${basedir}${file.separator}resources</directory>
      </resource>
    </resources>
    
    <plugins>
      <plugin>
        <groupId>com.igormaznitsa</groupId>
        <artifactId>mvn-golang-wrapper</artifactId>
        <version>${mvn.golang.version}</version>
        <extensions>true</extensions>
        <configuration>
          <goVersion>${go.sdk.version}</goVersion>
        </configuration>
        <executions>
          <execution>
            <id>default-build</id>
            <phase>none</phase>
          </execution>
          <execution>
            <id>default-mvninstall</id>
            <phase>none</phase>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
</project>

Step 3, create APP-1 and APP-2

Then we should create APP-1 and APP-2 pom.xml files in app-1 and app-2 folders. The Main trick here is to add common code into goPath, we do it through <addGoPath> section in mvn-golang configuration, the tag allows to add extra paths into GoPath and we just add <common>${basedir}/../common</common>, keep in mind that golang tool automatically adds '/src' so that you should provide just path to project (if it has regular structure of course).

for APP-1

<?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>com.igormaznitsa.test.mvngolang</groupId>
    <artifactId>mvngolang-multimodule-example</artifactId>
    <version>1.0-SNAPSHOT</version>
    <relativePath>../pom.xml</relativePath>
  </parent>

  <artifactId>app-1</artifactId>
  <packaging>mvn-golang</packaging>

  <name>App-1</name>

  <build>
    <sourceDirectory>${basedir}${file.separator}src</sourceDirectory>
    <directory>${basedir}${file.separator}bin</directory>
    
    <resources>
      <resource>
        <directory>${basedir}${file.separator}resources</directory>
      </resource>
    </resources>
    
    <plugins>
      <plugin>
        <groupId>com.igormaznitsa</groupId>
        <artifactId>mvn-golang-wrapper</artifactId>
        <version>${mvn.golang.version}</version>
        <extensions>true</extensions>
        <configuration>
          <goVersion>${go.sdk.version}</goVersion>
          <addToGoPath>
            <common>${basedir}/../common</common>
          </addToGoPath>
        </configuration>
      </plugin>
    </plugins>
  </build>

</project>

For App-2

<?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>com.igormaznitsa.test.mvngolang</groupId>
    <artifactId>mvngolang-multimodule-example</artifactId>
    <version>1.0-SNAPSHOT</version>
    <relativePath>../pom.xml</relativePath>
  </parent>

  <artifactId>app-2</artifactId>
  <packaging>mvn-golang</packaging>

  <name>App-2</name>

  <build>
    <sourceDirectory>${basedir}${file.separator}src</sourceDirectory>
    <directory>${basedir}${file.separator}bin</directory>
    
    <resources>
      <resource>
        <directory>${basedir}${file.separator}resources</directory>
      </resource>
    </resources>
    
    <plugins>
      <plugin>
        <groupId>com.igormaznitsa</groupId>
        <artifactId>mvn-golang-wrapper</artifactId>
        <version>${mvn.golang.version}</version>
        <extensions>true</extensions>
        <configuration>
          <goVersion>${go.sdk.version}</goVersion>
          <addToGoPath>
            <common>${basedir}/../common</common>
          </addToGoPath>
        </configuration>
      </plugin>
    </plugins>
  </build>

</project>