linux 环境下 用maven 创建项目(子项目分层创建)springMVC项目

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://xxlcube.blog.csdn.net/article/details/39182835

可以看下我的GITHUB的项目结构:https://github.com/cwtree/demoall

项目的整体目录结构

demo(demo.biz,demo.common,demo.config,demo.data,demo.web)

demo是最外层的目录,后面各个子项目分别负责业务层,工具、常用辅助类层,配置层,数据层,web层


本文章采用maven构建项目,maven的目录结构

src/main/java

src/test/java

src/main/resources


1、首先通过eclipse工具创建一个web project,这样会省去很多麻烦

将以下pom.xml文件放到demo.web下

web

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
 <parent>
 <groupId>demo.all</groupId>
 <version>0.1</version>
 <artifactId>demo.all</artifactId>
 <relativePath>../pom.xml</relativePath>
</parent>
  <groupId>demo.web</groupId>
  <artifactId>demo.web</artifactId>
  <packaging>war</packaging>
  <version>0.1</version>
  <name>demo.web</name>
<dependencies>
<dependency>
  <groupId>demo.common</groupId>
  <artifactId>demo.common</artifactId>
  <version>${pom.version}</version>
</dependency>
<dependency>
  <groupId>demo.biz</groupId>
  <artifactId>demo.biz</artifactId>
  <version>${pom.version}</version>
</dependency>		
<dependency>
  <groupId>demo.config</groupId>
  <artifactId>demo.config</artifactId>
  <version>${pom.version}</version>
</dependency>	
<dependency>
  <groupId>demo.data</groupId>
  <artifactId>demo.data</artifactId>
  <version>${pom.version}</version>
</dependency>	

</dependencies>
  <build>
	<resources>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.xml</include>
          <include>**/*.properties</include>
        </includes>
      </resource>
      <resource>
        <directory>src/main/resources</directory>
        <includes>
          <include>**/*.xml</include>
          <include>**/*.properties</include>
        </includes>
      </resource>
    </resources>
	<finalName>demo.web-${pom.version}</finalName>
	
  </build>
</project>

2、依次手动创建其余项目,手动建立MAVEN目录结构并创建pom.xml文件

biz

<span style="font-size:10px;"><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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
 <parent>
 <groupId>demo.all</groupId>
 <version>0.1</version>
 <artifactId>demo.all</artifactId>
 <relativePath>../pom.xml</relativePath>
</parent>
  <groupId>demo.biz</groupId>
  <artifactId>demo.biz</artifactId>
  <packaging>jar</packaging>
  <version>0.1</version>
  <name>demo.biz</name>
<dependencies>	
<dependency>
  <groupId>demo.common</groupId>
  <artifactId>demo.common</artifactId>
  <version>${pom.version}</version>
</dependency>
<dependency>
  <groupId>demo.data</groupId>
  <artifactId>demo.data</artifactId>
  <version>${pom.version}</version>
</dependency>

</dependencies>
  <build>
	<resources>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.xml</include>
          <include>**/*.properties</include>
        </includes>
      </resource>
      <resource>
        <directory>src/main/resources</directory>
        <includes>
          <include>**/*.xml</include>
          <include>**/*.properties</include>
        </includes>
      </resource>
    </resources>
	<finalName>demo.biz-${pom.version}</finalName>
  </build>
</project></span><strong style="font-size: 14px;">
</strong>
common   spring mvc  jar

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
 <parent>
 <groupId>demo.all</groupId>
 <version>0.1</version>
 <artifactId>demo.all</artifactId>
 <relativePath>../pom.xml</relativePath>
</parent>
  <groupId>demo.common</groupId>
  <artifactId>demo.common</artifactId>
  <packaging>jar</packaging>
  <version>0.1</version>
  <name>demo.common</name>
<dependencies>
    <dependency>
  <groupId>demo.config</groupId>
  <artifactId>demo.config</artifactId>
  <version>${pom.version}</version>
</dependency>	
<dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-context</artifactId>
</dependency>
<dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-aspects</artifactId>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <scope>provided</scope>
</dependency>
<dependency>
  <groupId>org.apache.velocity</groupId>
  <artifactId>velocity</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
</dependency>

<dependency>
  <groupId>log4j</groupId>
  <artifactId>log4j</artifactId>
</dependency>
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-log4j12</artifactId>
</dependency>
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
</dependency>
<dependency>
 <groupId>org.apache.commons</groupId>
 <artifactId>commons-lang3</artifactId>
</dependency>

<dependency>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
</dependency>


<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
</dependency>
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjrt</artifactId>
</dependency>
<dependency>
	<groupId>aopalliance</groupId>
	<artifactId>aopalliance</artifactId>
</dependency>

 <dependency>
  <groupId>javax.mail</groupId>
  <artifactId>mail</artifactId>
</dependency>

<dependency>
    <groupId>org.apache.ant</groupId>
    <artifactId>ant</artifactId>
</dependency>
<dependency>
  <groupId>net.spy</groupId>
  <artifactId>spymemcached</artifactId>
</dependency>	
<dependency>
  <groupId>joda-time</groupId>
  <artifactId>joda-time</artifactId>
</dependency>
</dependencies>
  <build>
	<resources>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.xml</include>
          <include>**/*.properties</include>
        </includes>
      </resource>
      <resource>
        <directory>src/main/resources</directory>
        <includes>
          <include>**/*.xml</include>
          <include>**/*.properties</include>
          <include>**/*.vm</include>
        </includes>
      </resource>
    </resources>
	<finalName>demo.common-${pom.version}</finalName>
  </build>
</project>
config

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
 <parent>
 <groupId>demo.all</groupId>
 <version>0.1</version>
 <artifactId>demo.all</artifactId>
 <relativePath>../pom.xml</relativePath>
</parent>
  <groupId>demo.config</groupId>
  <artifactId>demo.config</artifactId>
  <packaging>jar</packaging>
  <version>0.1</version>
  <name>demo.config</name>
<dependencies>	
</dependencies>
  <build>
	<resources>
	<resource> 
        <directory>src/main/resources</directory> 
        <filtering>true</filtering> 
        <includes>
          <include>**/*.properties</include>
          <include>**/*.xml</include>
          <include>**/*.xml.vm</include>
        </includes>
      </resource> 
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.xml</include>
          <include>**/*.properties</include>
        </includes>
      </resource>
      <resource>
        <directory>src/main/resources</directory>
        <includes>
          <include>**/*.xml</include>
          <include>**/*.properties</include>
          <include>**/*.pem</include>
          <include>**/*.vm</include>
        </includes>
      </resource>
    </resources>
	<finalName>demo.config-${pom.version}</finalName>
	<plugins>
      <plugin> 
   <artifactId>maven-antrun-plugin</artifactId>  
     <executions>     
        <execution>       
             <phase>compile</phase>      
                   <goals>          
                         <goal>run</goal>      
                   </goals>           
                   <configuration>     
                         <tasks>        
                              <echo message="*********************************************"/>    
                         </tasks>        
                   </configuration>      
        </execution>  
     </executions>
</plugin> 
    </plugins> 
  </build>
</project>
data

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
 <parent>
 <groupId>demo.all</groupId>
 <version>0.1</version>
 <artifactId>demo.all</artifactId>
 <relativePath>../pom.xml</relativePath>
</parent>
  <groupId>demo.data</groupId>
  <artifactId>demo.data</artifactId>
  <packaging>jar</packaging>
  <version>0.1</version>
  <name>demo.data</name>
<dependencies>	
<dependency>
  <groupId>demo.common</groupId>
  <artifactId>demo.common</artifactId>
  <version>${pom.version}</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.ibatis</groupId>
    <artifactId>ibatis-sqlmap</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-orm</artifactId>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
    <groupId>commons-dbcp</groupId>
    <artifactId>commons-dbcp</artifactId>
</dependency>
</dependencies>
  <build>
	<resources>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.xml</include>
          <include>**/*.properties</include>
        </includes>
      </resource>
      <resource>
        <directory>src/main/resources</directory>
        <includes>
          <include>**/*.xml</include>
          <include>**/*.properties</include>
        </includes>
      </resource>
    </resources>
	<finalName>demo.dal-${pom.version}</finalName>
  </build>
</project>

当然最主要的是最外层的pom.xml文件

<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>
  <groupId>demo.all</groupId>
  <version>0.1</version>
  <artifactId>demo.all</artifactId>
  <packaging>pom</packaging>
  <name>demo.all</name>
  <modules>
    <module>demo.web</module>
	<module>demo.biz</module>
    <module>demo.data</module>
    <module>demo.common</module>
    <module>demo.config</module>
  </modules>
  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  </properties>
  <repositories>
  </repositories>
  
  <dependencyManagement>
<dependencies>
<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.11</version>
</dependency>
<dependency>
 <groupId>org.apache.commons</groupId>
 <artifactId>commons-lang3</artifactId>
 <version>3.3.2</version>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.1.23</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.0.5.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>4.0.5.RELEASE</version>
</dependency>
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-aspects</artifactId>
	<version>4.0.5.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.apache.velocity</groupId>
  <artifactId>velocity</artifactId>
  <version>1.6.4</version>
</dependency>
<dependency>
  <groupId>log4j</groupId>
  <artifactId>log4j</artifactId>
  <version>1.2.12</version>
</dependency>
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-log4j12</artifactId>
  <version>1.5.11</version>
</dependency>
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
  <version>1.5.11</version>
</dependency>
<dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>servlet-api</artifactId>
   <version>2.5</version>
</dependency>
<dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context-support</artifactId>
   <version>4.0.5.RELEASE</version>
</dependency>

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>2.5.1</version>
</dependency>

<dependency>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>2.4.3</version>
</dependency>

<dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-jdbc</artifactId>
   <version>4.0.5.RELEASE</version>
</dependency>
<dependency>
   <groupId>org.apache.ibatis</groupId>
   <artifactId>ibatis-sqlmap</artifactId>
   <version>2.3.4.726</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-orm</artifactId>
    <version>3.1.1.RELEASE</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.31</version>
</dependency>
<dependency>
    <groupId>commons-dbcp</groupId>
    <artifactId>commons-dbcp</artifactId>
    <version>1.4</version>
</dependency>
<dependency>
   <groupId>org.aspectj</groupId>
   <artifactId>aspectjweaver</artifactId>
   <version>1.8.0</version>
</dependency>
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjrt</artifactId>
    <version>1.8.0</version>
</dependency>
<dependency>
	<groupId>aopalliance</groupId>
	<artifactId>aopalliance</artifactId>
	<version>1.0</version>
</dependency>

<dependency>
  <groupId>javax.mail</groupId>
  <artifactId>mail</artifactId>
  <version>1.5.0-b01</version>
</dependency>


<dependency>
    <groupId>org.apache.ant</groupId>
    <artifactId>ant</artifactId>
    <version>1.9.4</version>
</dependency>
<dependency>
  <groupId>net.spy</groupId>
  <artifactId>spymemcached</artifactId>
  <version>2.11.3</version>
</dependency>
<dependency>
  <groupId>joda-time</groupId>
  <artifactId>joda-time</artifactId>
  <version>2.4</version>
</dependency>
<dependency>  
  <groupId>org.apache.cxf</groupId>  
  <artifactId>cxf-rt-frontend-jaxws</artifactId>  
  <version>3.0.0</version>  
</dependency>  
<dependency>  
  <groupId>org.apache.cxf</groupId>  
  <artifactId>cxf-rt-databinding-xmlbeans</artifactId>  
  <version>3.0.0</version>  
</dependency>  
<dependency>  
  <groupId>org.apache.cxf</groupId>  
  <artifactId>cxf-rt-transports-http</artifactId>  
  <version>3.0.0</version>  
</dependency>  
<!-- Jetty is needed if you're using the CXFServlet -->  
<dependency>  
  <groupId>org.apache.cxf</groupId>  
  <artifactId>cxf-rt-transports-http-jetty</artifactId>  
  <version>3.0.0</version>  
</dependency>  
</dependencies>
</dependencyManagement>
  
<build>
<plugins>
	<plugin>
		<groupId>org.apache.maven.plugins</groupId>
		<artifactId>maven-compiler-plugin</artifactId>
		<version>2.3.2</version>
		<configuration>
			<source>1.7</source>
			<target>1.7</target>
		</configuration>
	</plugin>
</plugins>
</build>
</project>
整个目录结构都建立好了

下面在demo目录下执行如下命令:

mvn eclipse:eclipse -Dwtpversion=2.0 -DdownloadSources=true -DdownloadJavadocs=true 

[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] 
[INFO] demo.all ........................................... SUCCESS [  1.756 s]
[INFO] demo.config ........................................ SUCCESS [  0.376 s]
[INFO] demo.common ........................................ SUCCESS [  2.735 s]
[INFO] demo.biz ........................................... SUCCESS [ 10.359 s]
[INFO] demo.web ........................................... SUCCESS [  0.470 s]
[INFO] demo.data .......................................... SUCCESS [  0.593 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 17.523 s
[INFO] Finished at: 2014-09-10T17:25:12+08:00
[INFO] Final Memory: 11M/30M
[INFO] ------------------------------------------------------------------------
hadoop@hadoop-VirtualBox:~/mydisk/my_workspace/demo$ 

mvn clean install -Dmaven.test.skip

出现如下错误:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.2:war (default-war) on project demo.web: Error assembling WAR: webxml attribute is required (or pre-existing WEB-INF/web.xml if executing in update mode) -> [Help 1]

刚才,我们的WEB项目是通过eclipse创建的,而maven默认构建web项目时,

mavenweb项目默认的webroot是在src\main\webapp。如果在此目录下找不到web.xml就抛出以上的异常。

我们将WEB-INF的父目录拷贝到src/main下面,再次执行mvn clean install命令成功

<span style="font-size:10px;">[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] 
[INFO] demo.all ........................................... SUCCESS [  0.864 s]
[INFO] demo.config ........................................ SUCCESS [  2.262 s]
[INFO] demo.common ........................................ SUCCESS [  0.738 s]
[INFO] demo.data .......................................... SUCCESS [  0.482 s]
[INFO] demo.biz ........................................... SUCCESS [  0.396 s]
[INFO] demo.web ........................................... SUCCESS [  2.202 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7.433 s
[INFO] Finished at: 2014-09-11T08:30:15+08:00
[INFO] Final Memory: 10M/30M
[INFO] ------------------------------------------------------------------------</span><span style="font-size: 14px;">
</span>

执行完如上命令,如果没有错误,则可以看见完整的项目结构了,这是从eclipse里面import这些项目,就一目了然了
                                       
分层开发































展开阅读全文

没有更多推荐了,返回首页