Home  >  Article  >  Java  >  How to use maven to package the dubbo project into a jar package to run

How to use maven to package the dubbo project into a jar package to run

little bottle
little bottleforward
2019-04-30 10:41:452717browse

The method discussed in this article to use maven to package the dubbo project into a jar package for running is: use the maven packaging plug-in maven-jar-plugin to add some code at the end of the pom.xml file. Interested friends can take a look, I hope it can help you

maven packaging method

Use maven packaging plug-inmaven-jar-plugin
at Add the following code at the end of the pom.xml file.
maven-dependency-plugin refers to copying the dependent jar package to the specified directory
maven-resources-pluginCopying the 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>  
                        <!--   是否依赖外部jar包 -->
                            <addClasspath>true</addClasspath>  
                            <!-- 依赖外部jar包路径 -->
                            <classpathPrefix>lib/</classpathPrefix>  
 <!-- 启动函数 -->                           <mainClass>com.alibaba.dubbo.container.Main</mainClass>  
                        </manifest>  
                    </archive>  
                     <!-- 打包之后输出目录 --> 
                    <outputDirectory>${project.build.directory}/maven-archiver</outputDirectory>
<!--   剔除已打包的配置文件 -->
                     <excludes>
                         <exclude>*.*</exclude>
                         <exclude>config/*</exclude>
                         <exclude>config/tencent/*</exclude>
                         <exclude>META-INF/spring/*</exclude>
                     </excludes>
                </configuration>  
            </plugin>  

<!--             拷贝依赖的jar包到lib目录 -->
            <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>
<!--    拷贝依赖的资源文件包到resources目录 -->
            <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>
  • How to run the jar package

The directory structure after packaging
Contains the class file and the required configuration file information (excluding excludes configuration files)

How to use maven to package the dubbo project into a jar package to run

Contains the information required for operation 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
java -jar When executing the command, the Main-Class parameter in the directory META-INFMANIFEST.MF will be used. This is the entry point of the function specified during packaging.

java -jar x-0.0.1-SNAPSHOT.jar

java -cp
Startup 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 in this way
The configuration file address is an absolute path

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

-cp and -classpath have the same effect
java -cp is the dependency of the specified operation The path of other classes, usually class libraries, jar packages, etc. Multiple dependencies are separated by semicolons ";" on Window and ":" on linux .
Expressions support wildcards

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

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

Related tutorials: Java video tutorial

The above is the detailed content of How to use maven to package the dubbo project into a jar package to run. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete