spring-bootaxonmaven-shade-plugin

Springboot relocate models package


In my Spring Boot project, I have a package named com.example.models. I want to relocate this package to com.example.mymodels when running mvn install, how can rename package using maven?

I used the Axon Framework in my project and want to run separate, independent instances of the project but in same AxonServer. However, because the Commands have the same names, AxonServer distributes the requests among different instances. Now I want to solve this issue by changing the package. If you have a better solution, I'd appreciate it if you could let me know.


Solution

  • You can use the Maven Shade Plugin to relocate a package (i.e., rename it during build time) without modifying your source code directly. This is especially helpful when shading dependencies or repackaging internal classes.

    Add the following to your pom.xml:

    <build>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-shade-plugin</artifactId>
          <version>3.5.1</version>
          <executions>
            <execution>
              <phase>package</phase>
              <goals>
                <goal>shade</goal>
              </goals>
              <configuration>
                <relocations>
                  <relocation>
                    <pattern>com.example.models</pattern>
                    <shadedPattern>com.example.mymodels</shadedPattern>
                  </relocation>
                </relocations>
              </configuration>
            </execution>
          </executions>
        </plugin>
      </plugins>
    </build>
    

    Then run:

    mvn clean install

    This will generate a shaded JAR where all references to com.example.models are changed to com.example.mymodels.