《参考ibase4J搭建属于自己的分布式架构》---2.使用maven配置多个环境并运行项目

来源:互联网 发布:vision是什么软件 编辑:程序博客网 时间:2024/05/29 04:54

前言

首先声明,本次搭建框架使仿照ibase4J的架构进行搭建,结合了自己的一些理解做了一些修改,基本上都是复用ibase4J所采用的框架和代码。开此博客的目的是为了让自己以及大家更了解ibase4J的框架和代码,同时也能增加大家动手能力。博客主要为已经有一些java知识,但是还没有大坚果框架的同学学习,如果水平比较高的可以自行阅读ibase4j的源码。

正文

1.添加模块依赖

1.1facede模块依赖配置

facede模块仅依赖于base模块,在pom.xml文件做如下配置

<dependency>        <groupId>org.immortal4J</groupId>        <artifactId>immortal4J-base</artifactId>        <version>${project.version}</version>        <exclusions>            <exclusion>//表示facede只依赖与base,而不会依赖base的依赖(也就是传递依赖)                <groupId>*</groupId>                <artifactId>*</artifactId>            </exclusion>        </exclusions>    </dependency>

1.2service模块和web模块依赖配置

service模块和web模块都依赖于base模块和facade模块,pom.xml添加配置如下:

<dependency>      <groupId>org.immortal4J</groupId>      <artifactId>immortal4J-base</artifactId>      <version>1.1.0</version>      <scope>compile</scope>      <exclusions>        <exclusion>          <artifactId>*</artifactId>          <groupId>*</groupId>        </exclusion>      </exclusions>    </dependency>    <dependency>      <groupId>org.immortal4J</groupId>      <artifactId>immortal4J-sys-facade</artifactId>      <version>1.1.0</version>      <scope>compile</scope>      <exclusions>        <exclusion>          <artifactId>*</artifactId>          <groupId>*</groupId>        </exclusion>      </exclusions>    </dependency>

2.配置多个环境。开发/测试/生产环境

开发过程中,我们再不同的环境有不同的参数,为了方便切换,所以使用maven配置多个环境。
在主项目的pom中做如下配置

<profiles>        <profile>            <id>product</id>            <properties>            </properties>            <build>                <resources>                    <resource>                        <directory>src/main/java</directory>                        <includes>                            <include>**/*.properties</include>                            <include>**/*.xml</include>                        </includes>                        <filtering>true</filtering>                    </resource>                    <resource>                        <directory>src/main/resources</directory>                        <excludes>                              <exclude>config/local/*</exclude>                              <exclude>config/test/*</exclude>                          </excludes>                      </resource>                </resources>            </build>        </profile>        <profile>            <id>test</id>            <properties>            </properties>            <build>                <resources>                    <resource>                        <directory>src/main/java</directory>                        <includes>                            <include>**/*.properties</include>                            <include>**/*.xml</include>                        </includes>                        <filtering>true</filtering>                    </resource>                    <resource>                        <directory>src/main/resources</directory>                        <excludes>                              <exclude>config/local/*</exclude>                              <exclude>config/product/*</exclude>                          </excludes>                    </resource>                </resources>            </build>        </profile>        <profile>            <id>local</id>            <build>                <finalName>${project.name}</finalName>                <resources>                    <resource>                        <directory>src/main/java</directory>                        <includes>                            <include>**/*.properties</include>                            <include>**/*.xml</include>                        </includes>                        <filtering>true</filtering>                    </resource>                    <resource>                        <directory>src/main/resources</directory>                        <excludes>                              <exclude>config/test/*</exclude>                              <exclude>config/product/*</exclude>                          </excludes>                    </resource>                </resources>            </build>        </profile>    </profiles>

图中分别配置了product,test,local三个环境,分别代表生产,测试,本地开放的环境。三个环境的配置文件properties将分别放在resources/config下面的product,test,local文件夹下。

3.配置maven运行环境

3.1配置maven的tomcat插件

在local环境中的build节点下添加如下配置:

                <pluginManagement>                    <plugins>                        <plugin>                            <groupId>org.apache.tomcat.maven</groupId>                            <artifactId>tomcat7-maven-plugin</artifactId>                            <version>2.2</version>                            <executions>                                <execution>                                    <id>run-war-only</id>                                    <phase>pre-integration-test</phase>                                    <goals>                                        <goal>run-war-only</goal>                                    </goals>                                </execution>                            </executions>                            <configuration>                                <warDirectory>target/${project.name}</warDirectory>                                <path>/</path>                                <contextReloadable>true</contextReloadable>                                <uriEncoding>UTF-8</uriEncoding>                                <port>${server.port}</port>                                <url>http://localhost:${server.port}/manager</url>                                <server>tomcat</server>                                <username>admin</username>                                <password>admin</password>                                <contextReloadable>true</contextReloadable>                                <systemProperties>                                    <webapps>                                        <webapp>                                            <groupId>${project.name}</groupId>                                            <artifactId>${project.name}</artifactId>                                            <version>${project.version}</version>                                            <type>${project.packaging}</type>                                            <asWebapp>true</asWebapp>                                            <contextPath>/</contextPath>                                        </webapp>                                    </webapps>                                </systemProperties>                            </configuration>                        </plugin>                    </plugins>                </pluginManagement>

这一段代码是配置local环境运行时使用的tomcat,里面有一个server.port参数,代表的运行时的端口号。分别在service模块和web模块添加这个属性(为什么是这两个模块,因为这两个模块是最终生成war包并部署运行的模块)。

3.2配置端口号

在service和web中分别添加下面这段代码,并配置不同的端口号

  <properties>    <server.port>8091</server.port>  </properties>

4.使用maven命令启动项目

4.1启动web项目

4.1.1配置pom文件

在immortal4J项目根目录下添加pom-sys-web-server.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>    <parent>        <groupId>org.immortal4J</groupId>        <artifactId>immortal4J</artifactId>        <version>1.1.0</version>        <relativePath>./</relativePath>    </parent>    <artifactId>immortal4J-sys-service-server</artifactId>    <name>${project.artifactId}</name>    <packaging>pom</packaging>    <url>http://maven.apache.org</url>    <!-- clean package -P local tomcat7:run-war-only -f pom-sys-web-server.xml -->    <modules>        <module>immortal4J-base</module>        <module>immortal4J-sys-facade</module>        <module>immortal4J-sys-service</module>    </modules></project>

4.1.2 配置maven build

右键点击工具栏的run-》run configuration-》maven build-》new
按下图配置
这里写图片描述

按完run以后,maven开始打包项目并部署,如下图所示Running war on http://localhost:port/则部署成功。没有部署成功的查看日志找原因。

这里写图片描述

配置完成后还可以通过右键点击immortal4J-》run as -》maven build启动

4.2启动service项目

4.2.1配置pom文件

在immortal4J项目根目录下添加pom-sys-service-server.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>    <parent>        <groupId>org.immortal4J</groupId>        <artifactId>immortal4J</artifactId>        <version>1.1.0</version>        <relativePath>./</relativePath>    </parent>    <artifactId>immortal4J-sys-web-server</artifactId>    <name>${project.artifactId}</name>    <packaging>pom</packaging>    <url>http://maven.apache.org</url>    <!-- clean package -P local tomcat7:run-war-only -f pom-sys-web-server.xml -->    <modules>        <module>immortal4J-base</module>        <module>immortal4J-sys-facade</module>        <module>immortal4J-sys-service</module>    </modules></project>

4.2.2配置maven build

与web的配置一样,只是Goals变成了clean package -P local tomcat7:run-war-only -f pom-sys-web-server.xml

5.查看打包出来的war包

war生成在项目的target文件夹下,用360压缩打开可以看到,里面的结构
这里写图片描述
从图中可以看到,service项目的war里面包含了base模块个facade模块生成的jar包。
至此项目已经可以运行。

代码下载

下一篇我们将spring框架以及常用的jar包配置到项目中。

阅读全文
0 0
原创粉丝点击