maven构建dubbo服务的可执行jar包

dubbo提供者服务使用dubbo框架提供的Main方法类来运行:
dubbo框架提供:com.alibaba.dubbo.container.Main(可实现优雅关机ShutdownHook)


在提供者的pom.xml中配置以下maven插件:

<build>
        <finalName>dubbo-providerfinalName>  

        <resources>
            
            <resource>
                <targetPath>${project.build.directory}/classestargetPath>
                <directory>src/main/resourcesdirectory>
                <filtering>truefiltering>
                <includes>
                    
                    <include>**/*.xmlinclude>
                    <include>**/*.propertiesinclude>
                includes>
            resource>
            
            <resource>
                <targetPath>${project.build.directory}/classes/META-INF/springtargetPath>
                <directory>src/main/resources/springdirectory>
                <filtering>truefiltering>
                
                <includes>
                    <include>spring-application.xmlinclude>  
                includes>
            resource>
        resources>

        <pluginManagement>
            <plugins>
                
                <plugin>
                    <groupId>org.eclipse.m2egroupId>
                    <artifactId>lifecycle-mappingartifactId>
                    <version>1.0.0version>
                    <configuration>
                        <lifecycleMappingMetadata>
                            <pluginExecutions>
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>org.apache.maven.pluginsgroupId>
                                        <artifactId>maven-dependency-pluginartifactId>
                                        <versionRange>[2.0,)versionRange>
                                        <goals>
                                            <goal>copy-dependenciesgoal>
                                        goals>
                                    pluginExecutionFilter>
                                    <action>
                                        <ignore />
                                    action>
                                pluginExecution>
                            pluginExecutions>
                        lifecycleMappingMetadata>
                    configuration>
                plugin>
            plugins>
        pluginManagement>
        <plugins>
            
            
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-jar-pluginartifactId>
                <configuration>
                    
                    <classesDirectory>target/classes/classesDirectory>
                    <archive>
                        <manifest>
                            
                            <mainClass>com.alibaba.dubbo.container.MainmainClass>
                            
                            <useUniqueVersions>falseuseUniqueVersions>
                            <addClasspath>trueaddClasspath>
                            
                            <classpathPrefix>lib/classpathPrefix>
                        manifest>
                        <manifestEntries>
                            <Class-Path>.Class-Path>
                        manifestEntries>
                    archive>
                configuration>
            plugin>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-dependency-pluginartifactId>
                <executions>
                    <execution>
                        <id>copy-dependenciesid>
                        <phase>packagephase>
                        <goals>
                            <goal>copy-dependenciesgoal>
                        goals>
                        <configuration>
                            <type>jartype>
                            <includeTypes>jarincludeTypes>
                            <useUniqueVersions>falseuseUniqueVersions>
                            
                            <outputDirectory>
                                ${project.build.directory}/lib
                            outputDirectory>
                        configuration>
                    execution>
                executions>
            plugin>
        plugins>
    build>

注意在spring-application.xml中引入其他配置文件时要写为:

<import resource="classpath:spring/spring-mybatis.xml" />
    <import resource="classpath:spring/dubbo-provider.xml" />

因为spring-application.xml会被加载到${project.build.directory}/classes/META-INF/spring下,而不与引入的配置文件在同一目录下。


在对dubbo-provider服务打包成jar之前(maven install),需要将dubbo-provider服务依赖的工程先打包,在本地maven仓库生成相应的jar,这样dubbo-provider服务才会打包成功。最终在项目target目录下会生成lib文件夹和dubbo-provider.jar。lib文件夹存放的是dubbo-provider服务依赖的jar。
注:dubbo-provider.jar为什么能引用到lib文件夹内的jar,是因为dubbo-provider.jar的META-INF/MANIFEST.MF配置文件内说明了dubbo-provider服务依赖的jar。


运行dubbo-provider.jar:打开命令控制器,在dubbo-provider.jar所在的目录下输入java -jar dubbo-provider.jar & ,回车,出现下面的界面即运行成功:
maven构建dubbo服务的可执行jar包_第1张图片
管控台上成功显示提供者:
这里写图片描述

你可能感兴趣的:(dubbo)