maven2-学习文档

编译源代码:mvn compile (或者:mvn compiler:compile)   

编译测试代码:mvn test-compile    

运行测试:mvn test    

产生site:mvn site    

打包:mvn package    

在本地Repository中安装jar:mvn install    

清除产生的项目:mvn clean    

生成eclipse项目:mvn eclipse:eclipse   

生成idea项目:mvn idea:idea   

组合使用goal命令,如只打包不测试:mvn -Dtest package    

编译测试的内容:mvn test-compile   

只打jar包: mvn jar:jar   

只测试而不编译,也不测试编译:mvn test -skipping compile -skipping test-compile (这里要特别注意 -skipping 的灵活运用,当然也可以用于其他组合命令)   

清除eclipse的一些系统设置:mvn eclipse:clean  

生成eclipse工程

打开命令提示符,进入指定的项目目录D:/testapp ,

执行以下命令:

mvn archetype:create -DgroupId=com.mycompany.app
-DartifactId=my-webapp
-DarchetypeArtifactId=maven-archetype-webapp

其中 my-webapp是项目名,这样就生成了一个web项目,

然后 cd my-webapp进入此项目主目录,执行:mvn eclipse:eclipse

后项目my-webapp就变成了eclipse工程项目了。

用eclipse打开后,如果发现报如下错误:Unbound classpath variable: 'M2_REPO/junit/junit/3.8.1/junit-3.8.1.jar' in project my-webapp   ,这是因为在Eclipse 没有配置 M2_REPO 变量,配置步骤:window >> preferences >> Java >> Build Path >> Classpath Variables
新建一个 M2_REPO 的变量,变量值指向你系统的Maven2的数据仓库位置。

mvn archetype:create -DgroupId=com.hs.common
-DartifactId=common
-DarchetypeArtifactId=maven-archetype-webapp-

mvn install:install-file -DgroupId=com.danga -DartifactId=memcached -Dversion=2.0.1 -Dfile=java_memcached-release_2.0.1.jar -Dpackaging=jar \-DgeneratePom=true

 

maven添加非官方jar包到本地库    (安装到maven库:mvn install -Dmaven.test.skip=true (忽略测试代码))

mvn install:install-file
-DgroupId=<your_group_name> 
-DartifactId=<your_artifact_name> 
-Dversion=<snapshot> 
-Dfile=<path_to_your_jar_file> 
-Dpackaging=jar
-DgeneratePom=true 

安装客户自己的jar包

mvn install:install-file
-DgroupId=com.danga
-DartifactId=memcached
-Dversion=2.0.1
-Dfile=java_memcached-release_2.0.1.jar (或者/d:/java_memcached-release_2.0.1.jar)
-Dpackaging=jar -DgeneratePom=true

1.maven-helloword:
mvn archetype:create -DgroupId=com.demo -DartifactId=com.demo
-DpackageName=com.demo
将在cmd目录中所在的盘符位置创建一个 文件夹com.demo的文件夹
创建一个包名是com.demo的java类.类名是系统默认的App;
2.项目打包:
mvn jar:jar 把项目打成jar
mvn jar:test jar 把测试项目打成jar包
运行jar
java -cp simple-1.0-SNAPSHOT.jar com.demo.App
3.运行单元测试
 mvn test
 3.1忽略测试失败的情况
 <project>
  <build>
   <plugins>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
     <testFailureIgnore>true</testFailureIgnore>
    </configuration>
   </plugin>
   </plugins>
  </build>
 </project>

mvn test -Dmaven.test.failure.ignore=true;
3.2跳过单元测试
3.1忽略测试失败的情况
 <project>
  <build>
   <plugins>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
     <skip>true</skip>
    </configuration>
   </plugin>
   </plugins>
  </build>
 </project>
mvn install -Dmaven.test.skip=true;
4.maven 创建web项目
mvn archetype:create -DgroupId=com.mycompany.app -DartifactId=my-webapp -DarchetypeArtifactId=maven-archetype-webapp
创建的web项目名:my-webapp
web项目初始化后的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>com.mycompany.app</groupId>
  <artifactId>my-webapp</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>my-webapp Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <finalName>my-webapp</finalName>
  </build>
</project>
其中 finalName 表示的是生成的war包的名字
5.maven jetty插件配置
<project>
<build>
<plugins>
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>maven-jetty-plugin</artifactId>
                <version>6.1.7</version>
                <configuration>
                    <scanIntervalSeconds>5</scanIntervalSeconds>
                    <connectors>
                        <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
                            <port>8080</port>
                            <maxIdleTime>60000</maxIdleTime>
                        </connector>
                    </connectors>
                </configuration>  
            </plugin>
</plugins>
</build>
</project>
启动jetty:
 mvn clean -Dmaven.test.skip=true jetty:run
6.maven 多模块开发
 simple-model
 simple-weather
 simple-persist
 simple-webapp
 simple-command
simple-parent项目中有一个pom.xml这个xml引用五个子模块
simple-model,simple-weather,simple-persist,simple-webapp,simple-command

6.1顶级的pom.xml simple-parent.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>com.sonartype.mavenbook.ch07</groupId>
  <artifactId>simple-parent</artifactId>
  <packaging>pom</packaging>
  <version>1.0</version>
  <name>Parent Project</name>
  <modules>
   <module>simple-command</module>
 <module>simple-model</module>
 <module>simple-weather</module>
 <module>simple-persist</module>
 <module>simple-webapp</module>
  </modules>
  <build>
    <pluginManagement>
 <plugins>
  <plugin>
    <groupId>com.maven.plugins</groupId>
   <artifactId>maven-compiler-plugin</artifactId>
   <version>6.1.7</version>
   <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>

6.2simple-model 的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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
 <groupId>com.sonartype.mavenbook.ch07</groupId>
 <artifactId>simple-parent</artifactId>
 <version>1.0</version>
   </parent>

   <artifactId>simple-model</artifactId>

  <packaging>jar</packaging>

<name>simple object model</name>

    <dependencies>
 <dependency>
     <groupId>org.hibernate</groupId>
     <artifactId>hibernate-annotations</artifactId>
     <version>3.3.0.ga</version>
     <scope>test</scope>
 </dependency>
 <dependency>
     <groupId>org.hibernate</groupId>
     <artifactId>hibernate-commons-annotations</artifactId>
     <version>3.3.0.ga</version>
     <scope>test</scope>
 </dependency>
     </dependencies>
</project>


6.3 simple-weather 的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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
 <groupId>com.sonartype.mavenbook.ch07</groupId>
 <artifactId>simple-parent</artifactId>
 <version>1.0</version>
   </parent>

  <artifactId>simple-weather</artifactId>
  <packaging>jar</packaging>

<name>simple-weather</name>
    <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>
     </dependencies>
</project>

6.4 simple-persist 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>com.sonartype.mavenbook.ch07</groupId>
 <artifactId>simple-parent</artifactId>
 <version>1.0</version>
   </parent>

  <artifactId>simple-weather</artifactId>
  <packaging>jar</packaging>

<name>simple-persist</name>
    <dependencies>
 <dependency>
     <groupId>com.sonartype.mavenbook.ch07</groupId>
     <artifactId>simple-model</artifactId>
     <version>1.0</version>
 </dependency>
 <dependency>
     <groupId>org.hibernate</groupId>
     <artifactId>hibernate</artifactId>
     <version>3.2.5.ga</version>
     <exclusions>
     <exclusion>
    <groupId>javax.transaction</groupId>
    <artifactId>jta</artifactId>
     </exclusion>
     </exclusions>
 </dependency>
 <dependency>
     <groupId>org.hibernate</groupId>
     <artifactId>hibernate-annotations</artifactId>
     <version>3.3.0.ga</version>
     <scope>test</scope>
 </dependency>
 <dependency>
     <groupId>org.hibernate</groupId>
     <artifactId>hibernate-commons-annotations</artifactId>
     <version>3.3.0.ga</version>
     <scope>test</scope>
 </dependency>
 <dependency>
     <groupId>org.apache.geronimo.spece</groupId>
     <artifactId>geronimo-jta_1.1_spec</artifactId>
     <version>1.1</version>
 </dependency>
 <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring</artifactId>
     <version>2.0.7</version>
 </dependency>
     </dependencies>
</project>

6.5 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>com.sonartype.mavenbook.ch07</groupId>
 <artifactId>simple-parent</artifactId>
 <version>1.0</version>
   </parent>

  <artifactId>simple-webapp</artifactId>
  <packaging>war</packaging>

<name>simple-webapp</name>
    <dependencies>
     <dependency>
     <groupId>org.apache.geronimo.spece</groupId>
     <artifactId>geronimo-jta_1.1_spec</artifactId>
     <version>1.1</version>
 </dependency>
 <dependency>
     <groupId>com.sonartype.mavenbook.ch07</groupId>
     <artifactId>simple-weather</artifactId>
     <version>1.0</version>
 </dependency>
 <dependency>
     <groupId>com.sonartype.mavenbook.ch07</groupId>
     <artifactId>simple-persist</artifactId>
     <version>1.0</version>
 </dependency>
 <dependency>
     <groupId>org.hibernate</groupId>
     <artifactId>hibernate</artifactId>
     <version>3.2.5.ga</version>
     <exclusions>
     <exclusion>
    <groupId>javax.transaction</groupId>
    <artifactId>jta</artifactId>
     </exclusion>
     </exclusions>
 </dependency>
     </dependencies>
     <build>
 <finalName>simple-webapp</finalName>
 <plugins>
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>maven-jetty-plugin</artifactId>
                <version>6.1.7</version>
                <configuration>
                    <scanIntervalSeconds>5</scanIntervalSeconds>
                    <connectors>
                        <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
                            <port>8080</port>
                            <maxIdleTime>60000</maxIdleTime>
                        </connector>
                    </connectors>
                </configuration>
  <dependency>
     <groupId>hsqldb</groupId>
     <artifactId>hsqldb</artifactId>
     <version>1.8.0.7</version>
  </dependency>
            </plugin>
      <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>hibernate3-maven-plugin</artifactId>
                <version>2.0</version>
  <configuration>
                    <components>
   <component>
    <name>hbm2ddl<name>
    <implementation>annotationconfiguration</implementation>
   <component>
      </components>
                </configuration>
  <dependency>
     <groupId>hsqldb</groupId>
     <artifactId>hsqldb</artifactId>
     <version>1.8.0.7</version>
  </dependency>
            </plugin>
 </plugins>
 </build>
</project>

7.maven-clean 插件配置
<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>
   <build>
 <plugins>
  <plugin>
  <dependency>
     <groupId>maven-clean-plugin</groupId>
     <configuration>
   <filesets>
    <fileset>
     <directory>target-other<directory>
     <includes>
      <include>*.class</include>
     </includes>
    </fileset>
   </filesets>
     </configuration>
  </plugin>
 </plugins>
 </build>
</project>

 

 

--指定编辑jdk版本

<project>
 <build>
 <plugins>
            <plugin>     
     <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.5</source>
                    <target>1.5</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
 </plugins>
 </build>
</project>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值