设为首页 收藏本站
查看: 744|回复: 0

[经验分享] Maven学习日记(二)---MAVEN创建多模块的项目

[复制链接]

尚未签到

发表于 2017-2-28 10:18:43 | 显示全部楼层 |阅读模式
  手动构建多模块maven项目,这个simple-parent项目下有两个子模块,一个是jar包型的simple-weather和一个war型的simple-webapp
1.创建一个父的simple-parent项目
D:\>mvn archetype:create -DgroupId=org.sonatype.mavenbook.ch06 -DartifactId=simp
le-parent -DpackageName=org.sonatype.mavenbook -DarchetypeArtifactId=maven-arche
type-webapp
  2.修改里面的pom文件如下
<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>
<groupId>org.sonatype.mavenbook.ch06</groupId>
<artifactId>simple-parent</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<name>simple-parent Maven Webapp</name>
<url>http://maven.apache.org</url>
<modules>
<module>simple-weather</module>
<module>simple-webapp</module>
</modules>
<build>
    <pluginManagement>
    <plugins>
       <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
         <source>1.5</source>
         <target>1.5</target>
        </configuration>
       </plugin>
    </plugins>
    </pluginManagement>
</build>
<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
</dependencies>
</project>
3.在simple-parent下创建maven子项目是会出错.如下
D:\simple-parent>mvn archetype:create -DgroupId=org.sonatype.mavenbook.weather -
DartifactId=simple-weather
  org.apache.maven.reactor.MavenExecutionException: Could not find the model file
'D:\simple-parent\simple-weather\pom.xml'. for project unknown
        at org.apache.maven.DefaultMaven.getProjects(DefaultMaven.java:376)
        at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:289)
        at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:126)
        at org.apache.maven.cli.MavenCli.main(MavenCli.java:282)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
  将这个pom移到别的地方就能正常运行(simple-parent目录下的pom.xml)
  4.修改simple-weather中的pom文件
<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>org.sonatype.mavenbook.ch06</groupId>
    <artifactId>simple-parent</artifactId>
    <version>1.0</version>
</parent>
<artifactId>simple-weather</artifactId>
<packaging>jar</packaging>
  <name>Chapter 6 Simple Weather API</name>
  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <configuration>
            <testFailureIgnore>true</testFailureIgnore>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>
</build>
  <dependencies>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.14</version>
    </dependency>
    <dependency>
      <groupId>dom4j</groupId>
      <artifactId>dom4j</artifactId>
      <version>1.6.1</version>
    </dependency>
    <dependency>
      <groupId>jaxen</groupId>
      <artifactId>jaxen</artifactId>
      <version>1.1.1</version>
    </dependency>
    <dependency>
      <groupId>velocity</groupId>
      <artifactId>velocity</artifactId>
      <version>1.5</version>
    </dependency>
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-io</artifactId>
      <version>1.3.2</version>
      <scope>test</scope>
    </dependency>
</dependencies>
</project>
  其它包中的内容直接copy过来
  5.创建simple-webapp
D:\simple-parent>mvn archetype:create -DgroupId=org.sonatype.mavenbook.web -Dart
ifactId=simple-webapp -DpackageName=org.sonatype.mavenbook -DarchetypeArtifactId
=maven-archetype-webapp
  6.修改simple-webapp中的pom文件
<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>org.sonatype.mavenbook.ch06</groupId>
    <artifactId>simple-parent</artifactId>
    <version>1.0</version>
</parent>
  <artifactId>simple-webapp</artifactId>
<packaging>war</packaging>
<name>simple-webapp Maven Webapp</name>
<dependencies>
    <dependency>
      <groupId>org.apache.geronimo.specs</groupId>
      <artifactId>geronimo-servlet_2.4_spec</artifactId>
      <version>1.1.1</version>
    </dependency>
    <dependency>
      <groupId>org.sonatype.mavenbook.ch06</groupId>
      <artifactId>simple-weather</artifactId>
      <version>1.0</version>
    </dependency>
</dependencies>
<build>
    <finalName>simple-webapp</finalName>
    <plugins>
      <plugin>
        <groupId>org.mortbay.jetty</groupId>
        <artifactId>maven-jetty-plugin</artifactId>
      </plugin>
    </plugins>
</build>
</project>
7.从sample中copy包源码与web.xml
  8.进入simple-parent中构建多模块项目mvn clean install
  9.运行mvn jetty:run
  10.可以构建为eclipse项目mvn eclipse:eclipse
  11.访问http://localhost:8080/simple-webapp/weather?zip=110
  相关资料:
参与书籍:maven权威指南
链接:http://www.sonatype.com/books/maven-book/reference_zh/public-book.html
书中源码下载地址:http://www.sonatype.com/book/mvn-examples-1.0.zip

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-348295-1-1.html 上篇帖子: 新浪云部署java web程序 注意事项 下篇帖子: Spark编译安装和运行
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表