分享

经典的Maven-Web框架实例

 且看且珍惜 2014-09-23
一、安装Maven并配置settings
1.settings.xml内容如下:
<settings>
    <profiles>
        <profile>
            <id>dev0</id>
            <repositories>
                <repository>
                    <id>remote-nexus</id>
                    <url>http://maven./nexus/content/groups/public/</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
            </repository>
           </repositories>
        </profile>
         <profile>
            <id>dev1</id>
            <repositories>
                <repository>
                    <id>local-nexus</id>
                    <url>http://maven./nexus/content/repositories/releases/</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </repository>
            </repositories>
        </profile>
    </profiles>
    <activeProfiles>
        <activeProfile>dev0</activeProfile>
        <activeProfile>dev1</activeProfile>
    </activeProfiles>
</settings>
二、搭建公共Maven项目WebParent,供其他Maven项目使用。项目结构如下图所示:

1.配置pom.xml文件,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE project [
        <!ELEMENT project (modelVersion|groupId|artifactId|version|description|developers|packaging|properties|dependencies|distributionManagement|build)*>
        <!ATTLIST project
                xmlns CDATA #REQUIRED
                xmlns:xsi CDATA #REQUIRED
                xsi:schemaLocation CDATA #REQUIRED>
        <!ELEMENT modelVersion (#PCDATA)>
        <!ELEMENT groupId (#PCDATA)>
        <!ELEMENT artifactId (#PCDATA)>
        <!ELEMENT version (#PCDATA)>
        <!ELEMENT description (#PCDATA)>
        <!ELEMENT developers (developer)*>
        <!ELEMENT developer (name)*>
        <!ELEMENT name (#PCDATA)>
        <!ELEMENT packaging (#PCDATA)>
        <!ELEMENT properties (struts.version|springframework.version)*>
        <!ELEMENT struts.version (#PCDATA)>
        <!ELEMENT springframework.version (#PCDATA)>
        <!ELEMENT dependencies (dependency)*>
        <!ELEMENT dependency (groupId|artifactId|version|classifier|type|scope)*>
        <!ELEMENT classifier (#PCDATA)>
        <!ELEMENT type (#PCDATA)>
        <!ELEMENT scope (#PCDATA)>
        <!ELEMENT distributionManagement (repository|snapshotRepository)*>
        <!ELEMENT repository (id|name|url)*>
        <!ELEMENT id (#PCDATA)>
        <!ELEMENT url (#PCDATA)>
        <!ELEMENT snapshotRepository (id|name|url)*>
        <!ELEMENT build (finalName|sourceDirectory|outputDirectory|resources|plugins)*>
        <!ELEMENT finalName (#PCDATA)>
        <!ELEMENT sourceDirectory (#PCDATA)>
        <!ELEMENT outputDirectory (#PCDATA)>
        <!ELEMENT resources (resource)*>
        <!ELEMENT resource (directory|excludes)*>
        <!ELEMENT directory (#PCDATA)>
        <!ELEMENT excludes (exclude)*>
        <!ELEMENT exclude (#PCDATA)>
        <!ELEMENT plugins (plugin)*>
        <!ELEMENT plugin (groupId|artifactId|version|configuration|executions)*>
        <!ELEMENT configuration (filesets|webappDirectory|warSourceDirectory|warSourceIncludes|source|target|encoding|includes|additionalProjectnatures|additionalBuildcommands)*>
        <!ELEMENT filesets (fileset)*>
        <!ELEMENT fileset (directory|followSymlinks|useDefaultExcludes|includes)*>
        <!ELEMENT followSymlinks (#PCDATA)>
        <!ELEMENT useDefaultExcludes (#PCDATA)>
        <!ELEMENT includes (include)*>
        <!ELEMENT include (#PCDATA)>
        <!ELEMENT webappDirectory (#PCDATA)>
        <!ELEMENT warSourceDirectory (#PCDATA)>
        <!ELEMENT warSourceIncludes (#PCDATA)>
        <!ELEMENT source (#PCDATA)>
        <!ELEMENT target (#PCDATA)>
        <!ELEMENT encoding (#PCDATA)>
        <!ELEMENT executions (execution)*>
        <!ELEMENT execution (id|phase|goals)*>
        <!ELEMENT phase (#PCDATA)>
        <!ELEMENT goals (goal)*>
        <!ELEMENT goal (#PCDATA)>
        <!ELEMENT additionalProjectnatures (projectnature)*>
        <!ELEMENT projectnature (#PCDATA)>
        <!ELEMENT additionalBuildcommands (buildcommand)*>
        <!ELEMENT buildcommand (#PCDATA)>
        ]>
<project xmlns="http://maven./POM/4.0.0"
         xmlns:xsi="http://www./2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven./POM/4.0.0 http://maven./xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>fangtoon.com</groupId>
    <artifactId>WebParent</artifactId>
    <version>1.0.0</version>
    <description>xxx集团-xxx系统基础架构</description>
    <developers>
        <developer>
            <name>赵腾</name>
        </developer>
    </developers>
    <packaging>pom</packaging>
    <!-- 依赖库版本配置 -->
    <properties>
        <struts.version>2.3.15.1</struts.version>
        <springframework.version>3.2.11.RELEASE</springframework.version>
    </properties>
    <dependencies>
        <!-- spring mvc相关包 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${springframework.version}</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${springframework.version}</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${springframework.version}</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${springframework.version}</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-remoting</artifactId>
            <version>2.0.8</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <!--json-lib-->
        <dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <version>2.4</version>
            <classifier>jdk15</classifier>
        </dependency>
        <!-- struts相关包 -->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>${struts.version}</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-spring-plugin</artifactId>
            <version>${struts.version}</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-json-plugin</artifactId>
            <version>${struts.version}</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <!-- 消息队列相关包 -->
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-core</artifactId>
            <version>5.7.0</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.geronimo.bundles</groupId>
            <artifactId>aspectjrt</artifactId>
            <version>1.6.8_2</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.geronimo.bundles</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.6.8_2</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <!-- 配置资源相关包 -->
        <dependency>
            <groupId>commons-configuration</groupId>
            <artifactId>commons-configuration</artifactId>
            <version>1.6</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <!-- 远程调用相关包 -->
        <dependency>
            <groupId>com.caucho</groupId>
            <artifactId>hessian</artifactId>
            <version>3.1.3</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>commons-httpclient</groupId>
            <artifactId>commons-httpclient</artifactId>
            <version>3.1</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <!-- 单元测试包 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <type>jar</type>
            <scope>test</scope>
        </dependency>
        <!-- 其他包 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.7</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.1</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.kafka</groupId>
            <artifactId>kafka_2.9.2</artifactId>
            <version>0.8.1.1</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
    </dependencies>
    <!-- 部署到maven中心 -->
    <distributionManagement>
        <repository>
            <id>nexus-releases</id>
            <name>Nexus Releases Repository</name>
            <url>http://maven./nexus/content/repositories/releases/</url>
        </repository>
        <snapshotRepository>
            <id>nexus-snapshots</id>
            <name>Nexus Snapshots Repository</name>
            <url>http://maven./nexus/content/repositories/snapshots/</url>
        </snapshotRepository>
    </distributionManagement>
    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <!-- 清除 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-clean-plugin</artifactId>
                <version>2.5</version>
                <configuration>
                    <filesets>
                        <!-- 清除classes -->
                        <fileset>
                            <directory>webapp/WEB-INF/classes</directory>
                            <followSymlinks>false</followSymlinks>
                            <useDefaultExcludes>true</useDefaultExcludes>
                        </fileset>
                        <!-- 清除lib下jar包 -->
                        <fileset>
                            <directory>webapp/WEB-INF/lib</directory>
                            <followSymlinks>false</followSymlinks>
                            <useDefaultExcludes>true</useDefaultExcludes>
                        </fileset>
                        <!-- 清除压缩js -->
                        <fileset>
                            <directory>webapp/js</directory>
                            <followSymlinks>false</followSymlinks>
                            <useDefaultExcludes>true</useDefaultExcludes>
                            <includes>
                                <include>*-min.js</include>
                                <include>${project.artifactId}-${project.version}.js</include>
                            </includes>
                        </fileset>
                    </filesets>
                </configuration>
            </plugin>
            <!-- 打war包 -->
            <plugin>
                <version>2.2</version>
                <artifactId>maven-war-plugin</artifactId>
                <configuration>
                    <webResources>
                        <resource>
                            <filtering>true</filtering>
                            <directory>src/main/webapp</directory>
                            <includes>
                                <include>**/*.xml</include>
                            </includes>
                        </resource>
                    </webResources>
                </configuration>
            </plugin>
            <!-- 编译 -->
            <plugin>
                <version>2.3.2</version>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <encoding>UTF-8</encoding>
                    <includes>
                        <include>**/*.java</include>
                    </includes>
                </configuration>
            </plugin>
            <!--  package jar on package -->
            <plugin>
                <version>2.4</version>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <includes>
                        <include>**/*.class</include>
                        <include>**/*.properties</include>
                    </includes>
                </configuration>
                <executions>
                    <execution>
                        <id>make-a-jar</id>
                        <phase>compile</phase>
                        <goals>
                            <goal>jar</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!-- 生成eclipse工程 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-eclipse-plugin</artifactId>
                <version>2.9</version>
                <configuration>
                    <additionalProjectnatures>
                        <projectnature>org.eclipse.jdt.core.javanature</projectnature>
                    </additionalProjectnatures>
                    <additionalBuildcommands>
                        <buildcommand>org.eclipse.jdt.core.javabuilder</buildcommand>
                    </additionalBuildcommands>
                </configuration>
            </plugin>
            <!-- 生成intellijj idea工程 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-idea-plugin</artifactId>
                <version>2.2.1</version>
            </plugin>
        </plugins>
    </build>

</project>

三、搭建一般的Maven项目WebPlatformDemo。
整个项目一般分为三个模块,分别为接口定义模块Interface---接口层、接口实现模块Implement---服务实现层、用户接口模块(使用接口模块)UserInterface---前端展示层,框架结构如下图所示:






1.项目WebPlatformDemo(Project)的pom.xml文件配置,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven./POM/4.0.0"
         xmlns:xsi="http://www./2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven./POM/4.0.0 http://maven./xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <description>平台级工程-包含:接口层、服务实现层、前端展示层</description>
    <groupId>fangtoon.com</groupId>
    <artifactId>WebPlatformDemo</artifactId>
    <packaging>pom</packaging>
    <version>1.0.0</version>
    <parent>
        <artifactId>WebParent</artifactId>
        <groupId>fangtoon.com</groupId>
        <version>1.0.0</version>
    </parent>
    <!-- 本项目包含的模块 -->
    <modules>
        <module>Interface</module>
        <module>Implement</module>
        <module>UserInterface</module>
    </modules>
    <build>
        <plugins>
            <!-- 单元测试(开启 skipTests false,调过 skipTests true) -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.5</version>
                <configuration>
                    <skipTests>true</skipTests>
                    <includes>
                        <include>**/*Test.java</include>
                    </includes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

2.模块Interface的pom.xml文件配置,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven./POM/4.0.0"
         xmlns:xsi="http://www./2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven./POM/4.0.0 http://maven./xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <description>平台级工程-接口定义层</description>
    <artifactId>${parent.artifactId}Interface</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>
    <parent>
        <artifactId>WebPlatformDemo</artifactId>
        <groupId>fangtoon.com</groupId>
        <version>1.0.0</version>
    </parent>
    <build>
        <!-- 接口jar必选携带版本号 -->
        <finalName>${project.artifactId}-${project.version}</finalName>
    </build>
    <!-- 特有依赖包 -->
    <dependencies>
        <dependency>
            <groupId>fangtoon.com</groupId>
            <artifactId>base-interface</artifactId>
            <version>1.0.0</version>
        </dependency>
    </dependencies>

</project>

3.模块Implement的pom.xml文件配置,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven./POM/4.0.0"
         xmlns:xsi="http://www./2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven./POM/4.0.0 http://maven./xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <description>平台级工程-服务实现层</description>
    <packaging>war</packaging>
    <artifactId>${parent.artifactId}Implement</artifactId>
    <parent>
        <artifactId>WebPlatformDemo</artifactId>
        <groupId>fangtoon.com</groupId>
        <version>1.0.0</version>
    </parent>
    <!-- 打包环境参数配置 -->
    <profiles>
        <!-- 本地环境 -->
        <profile>
            <id>local</id>
            <properties>
                <webAppRootKey>${project.artifactId}</webAppRootKey>
                <databaseBean>jdbc</databaseBean>
            </properties>
        </profile>
        <!-- 开发环境 -->
        <profile>
            <id>development</id>
            <properties>
                <webAppRootKey>${project.artifactId}</webAppRootKey>
                <databaseBean>jndi</databaseBean>
            </properties>
        </profile>
        <!-- 测试环境 -->
        <profile>
            <id>test</id>
            <properties>
                <webAppRootKey>${project.artifactId}</webAppRootKey>
                <databaseBean>jndi</databaseBean>
            </properties>
        </profile>
        <!-- 生产环境 -->
        <profile>
            <id>production</id>
            <properties>
                <webAppRootKey>${project.artifactId}</webAppRootKey>
                <databaseBean>jndi</databaseBean>
            </properties>
        </profile>
    </profiles>
    <!-- 特有依赖包 -->
    <dependencies>
        <!-- 本项目接口包 -->
        <dependency>
            <groupId>fangtoon.com</groupId>
            <artifactId>${parent.artifactId}Interface</artifactId>
            <version>1.0.0</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <!-- 思源DAO层封装包-->
        <dependency>
            <groupId>fangtoon.com</groupId>
            <artifactId>fangtoon-dao</artifactId>
            <version>1.0.0</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <!-- 思源缓存封装包-->
        <dependency>
            <groupId>fangtoon.com</groupId>
            <artifactId>fangtoon-cache</artifactId>
            <version>1.0.0</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
        <!-- ibatis包-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-ibatis</artifactId>
            <version>2.0.8</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
    </dependencies>
</project>

4.模块UserInterface的pom.xml文件配置,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven./POM/4.0.0"
         xmlns:xsi="http://www./2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven./POM/4.0.0 http://maven./xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <description>平台级工程-Web展示层、后台管理页面</description>
    <packaging>war</packaging>
    <artifactId>${parent.artifactId}UI</artifactId>
    <parent>
        <artifactId>WebPlatformDemo</artifactId>
        <groupId>fangtoon.com</groupId>
        <version>1.0.0</version>
    </parent>

    <!-- 打包环境参数配置 -->
    <profiles>
        <!-- 开发环境 -->
        <profile>
            <id>development</id>
            <properties>
                <webAppRootKey>${project.artifactId}</webAppRootKey>
            </properties>
        </profile>
        <!-- 测试环境 -->
        <profile>
            <id>test</id>
            <properties>
                <webAppRootKey>${project.artifactId}</webAppRootKey>
            </properties>
        </profile>
        <!-- 生产环境 -->
        <profile>
            <id>production</id>
            <properties>
                <webAppRootKey>${project.artifactId}</webAppRootKey>
            </properties>
        </profile>
    </profiles>
    <!-- 特有依赖包 -->
    <dependencies>
        <!-- 本项目接口包 -->
        <dependency>
            <groupId>fangtoon.com</groupId>
            <artifactId>${parent.artifactId}Interface</artifactId>
            <version>1.0.0</version>
            <type>jar</type>
            <scope>compile</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <!-- YUI Compressor Maven压缩插件 -->
            <plugin>
                <groupId>net.alchim31.maven</groupId>
                <artifactId>yuicompressor-maven-plugin</artifactId>
                <version>1.3.0</version>
                <executions>
                    <execution>
                        <phase>compile</phase>
                        <goals>
                            <goal>compress</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <!-- 读取js,css文件采用UTF-8编码 -->
                    <encoding>UTF-8</encoding>
                    <!-- 不显示js可能的错误 -->
                    <jswarn>false</jswarn>
                    <!-- 若存在已压缩的文件,会先对比源文件是否有改动。有改动便压缩,无改动就不压缩 -->
                    <force>false</force>
                    <!-- 在指定的列号后插入新行 -->
                    <linebreakpos>-1</linebreakpos>
                    <!-- 压缩之前先执行聚合文件操作 -->
                    <preProcessAggregates>true</preProcessAggregates>
                    <!-- 压缩后保存文件后缀 -->
                    <suffix>-min</suffix>
                    <!-- 源目录,即需压缩的根目录 -->
                    <sourceDirectory>src/main/webapp/js</sourceDirectory>
                    <!-- 压缩js文件 -->
                    <includes>
                        <include>**/${project.artifactId}-${project.version}.js</include>
                    </includes>
                    <!-- 以下目录和文件不会被压缩 -->
                    <excludes>
                        <exclude>**/*-min.js</exclude>
                    </excludes>
                    <!-- 压缩后输出文件目录 -->
                    <outputDirectory>src/main/webapp/js</outputDirectory>

                    <!-- 聚合文件 -->
                    <aggregations>
                        <aggregation>
                            <!-- 合并每一个文件后插入一新行 -->
                            <insertNewLine>true</insertNewLine>
                            <!-- 需合并文件的根文件夹 -->
                            <inputDir>src/main/webapp/js</inputDir>
                            <!-- 最终合并的输出文件 -->
                            <output>src/main/webapp/js/${project.artifactId}-${project.version}.js</output>
                            <!-- 把以下js文件合并成一个js文件,是按顺序合并的 -->
                            <includes>
                                <include>**/*.js</include>
                            </includes>
                            <excludes>
                                <exclude>**/*-min.js</exclude>
                            </excludes>
                        </aggregation>
                    </aggregations>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约