Run dubbo project as jar package with maven

Keywords: Java Maven Dubbo Apache

  • maven packaging

Package the plug-in maven-jar-plugin with maven
Add the following code at the end of the pom.xml file.
maven-dependency-plugin refers to copying a dependent jar package to a specified directory
maven-resources-plugin copies dependent resources to the specified directory

    <build>
        <plugins>
             <plugin>  
                <groupId>org.apache.maven.plugins</groupId>  
                <artifactId>maven-jar-plugin</artifactId>  
                <version>2.6</version>  
                <configuration>  
                    <archive>  
                        <manifest>  
                        <!--   Whether to rely on external jar package -->
                            <addClasspath>true</addClasspath>  
                            <!-- Dependent on External jar package path -->
                            <classpathPrefix>lib/</classpathPrefix>  
 <!-- Startup function -->                           <mainClass>com.alibaba.dubbo.container.Main</mainClass>  
                        </manifest>  
                    </archive>  
                     <!-- Output directory after packaging --> 
                    <outputDirectory>${project.build.directory}/maven-archiver</outputDirectory>
<!--   Remove packaged configuration files -->
                     <excludes>
                         <exclude>*.*</exclude>
                         <exclude>config/*</exclude>
                         <exclude>config/tencent/*</exclude>
                         <exclude>META-INF/spring/*</exclude>
                     </excludes>
                </configuration>  
            </plugin>  

<!--             Copy dependent jar Package to lib Catalog -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.8</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>
                                ${project.build.directory}/maven-archiver/lib
                            </outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
<!--    Copy dependent resource files to resources Catalog -->
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-resources</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/maven-archiver/resources</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>${basedir}/src/main/resources</directory>
                                    <filtering>true</filtering>

                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
  • Running jar packages

Packed directory structure
Includes the class file with the required profile information (excludes is not included)

The information needed to run is included in MANIFEST.MF

Class-Path: lib/commons-beanutils-1.8.3.jar lib/commons-betwixt-0.8.jar
...
Main-Class: com.alibaba.dubbo.container.Main

java -jar
Start Command
When java-jar executes a command, it uses the Main-Class parameter in the directory META-INFMANIFEST.MF, which specifies the entry to the function when packaging.

java -jar x-0.0.1-SNAPSHOT.jar

java -cp
Start Command

java -cp .:x-0.0.1-SNAPSHOT.jar packname.mainclassname 

Because I need to reference external resources files in my project, I usually start them this way
Profile address is an absolute path

java -cp /Users/username/resources:dubbo-service-1.0.jar com.alibaba.dubbo.container.Main 

-cp and-classpath are the same
Java-cp is the path that specifies the other classes on which to run, typically class libraries, jar packages, etc. Multiple dependencies are separated by a semicolon';'on Window and':' on linux.
Expressions support wildcards

java -cp .:/Users/username/*.jar   packname.mainclassname 
  • Make a jar package

If you need to run all dependent jar packages in one jar package, you can use the plug-in maven-shade-plugin.
The main function of this plug-in is to package the dependent jar package into the current jar package, and rename the class when packaging, which can resolve the multi-version conflict of the jar package and query the official plug-in documentation.

Reference Documents
Official maven plug-in documentation

Posted by andycole on Tue, 30 Apr 2019 23:30:38 -0700