1、部署jar包到私服或发布东西到私服:
1.1 先要在pom.xml文件里加
<distributionManagement>
<repository>
<id>nexus_RELEASE</id>
<name>nexus_RELEASE</name>
<url>
http://localhost:8081/nexus/content/repositories/releases/
</url>
</repository>
<snapshotRepository>
<id>nexus_SNAPSHOT</id>
<name>nexus_SNAPSHOT</name>
<url>
http://localhost:8081/nexus/content/repositories/snapshots/
</url>
</snapshotRepository>
</distributionManagement>
不然执行deploy命令的时候会报Deployment failed: repository element was not specified in the POM inside distributionManagement element or in -DaltDeploymentRepository=id。
1.2然后要开启权限:
先要在网页上Releases和Snapshots里的配置里开启Deployment Policy 为true,再
在setting.xml里配置server:
<server>
<id>nexus_RELEASE</id>
<username>deployment</username>
<password>deployment123</password>
</server>
<server>
<id>nexus_SNAPSHOT</id>
<username>deployment</username>
<password>deployment123</password>
</server>
注意这里的id要和<distributionManagement>里的id一致。
2、插件
Maven中所有的功能都是由插件来完成的
插件在本地仓库中的默认插件目录:E:\apache-maven-3.2.5_repository\org\apache\maven\plugins
在pom.xml里设置,要放在<build> <plugins> </plugins></build>中。执行的命令可以去查看Maven的官网。
2.1常用的插件:
<!-- javadoc插件 --> 命令是javadoc:javadoc 前一个javadoc说明用的是什么插件,后一个是说明用的是这个插件里的哪个命令。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.10.2</version>
<configuration>
<charset>UTF-8</charset>
<encoding>UTF-8</encoding>
<docencoding>UTF-8</docencoding>
</configuration>
</plugin>
<!-- source插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>test_source</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- 拷贝maven依赖的jar插件到指定的目录 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>lib</outputDirectory> <overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>
false
</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<!-- 它可以配置成能定期扫描Web应用的任何改变和自动部署Web应用,充当了tomcat的功能 -->
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.17</version>
<configuration>
<webDefaultXml>src/webdefault.xml</webDefaultXml>
<webAppSourceDirectory>
WebRoot
</webAppSourceDirectory>
<scanIntervalSeconds>3</scanIntervalSeconds>
<contextPath>/Maven_Web</contextPath>
<connectors>
<connector
implementation="org.mortbay.jetty.nio.SelectChannelConnector">
<port>5050</port>
</connector>
</connectors>
</configuration>
</plugin>
注意:要将webdefault.xml拷到相应的目录下,执行时的命令为jetty:run.
在网页查看时的URL为:http://localhost:5050/Maven_Web/index.jsp可根据插件的配置得出。
<contextPath>/Ajax1</contextPath>里的值要改成相应工程的名字。
在使用jetty的过程中,如果有出现OutOfMemoryError:PermGen space的错误。
解决:设置debug as ---->run configuragtions-->jre标签下的属性VM arguments:
-server -Xms256m -Xmx256m -XX:PermSize=256m -XX:MaxPermSize=256m
2.2:为了避免每一个插件都要单独去执行,所以我们进行了绑定:
我们要加上
<executions>
<execution>
<id>test_javadoc</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
其中<phase>package</phase>是生命周期,<goal>jar</goal>是执行的命令。
就变成如下:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.10.2</version>
<configuration>
<charset>UTF-8</charset>
<encoding>UTF-8</encoding>
<docencoding>UTF-8</docencoding>
</configuration>
<executions>
<execution>
<id>test_javadoc</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
3:演示聚合和继承:
3.1 先建三个MavenJava工程分别为Maven_Dao,Maven_Service,Maven_Total。再建一个Maven的web工程。
Maven_Total里只要留Pom.xml就好了,也可以留下JRE和Maven依赖包。
并且Maven_total里的pom.xml里的<packaging>jar</packaging>要改为<packaging>pom</packaging>,
还要加上:
<modules>
<module>../Maven_Dao</module>
<module>../Maven_Service</module>
<module>../Maven_Web</module>
</modules>
上面的步骤就完成了聚合即把多个工程放到一个工程里进行打包、安装、发布等命令。
3.2:继承:父工程里统一定义,子工程引用。
先在父类的配置文件中,统一定义依赖与插件的版本,也就是在<dependencyManagement><dependencies></dependencies></dependencyManagement>里定义依赖
比如:
<!-- 在父类的配置文件中,统一定义依赖与插件的版本 -->
<!-- 定义依赖 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
</dependencies>
</dependencyManagement>
<!-- 定义插件 -->
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.10.2</version>
<configuration>
<charset>UTF-8</charset>
<encoding>UTF-8</encoding>
<docencoding>UTF-8</docencoding>
</configuration>
<executions>
<execution>
<id>attach-javadocs</id>
<phase>package</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>
</build>
然后在子类的配置文件中定义引用的父类
比如:
<!-- 引用Maven的父类配置 -->
<parent>
<groupId>Maven_Total</groupId>
<artifactId>Maven_Total</artifactId>
<version>0.0.1</version>
<relativePath>../Maven_Total/pom.xml</relativePath>
</parent>
再在子类中引用依赖:这时不用写版本
<!-- 引用Maven的父类的依赖 -->
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</dependency>
</dependencies>
<!-- 引用Maven的父类插件 -->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
</plugin>
</plugins>
</build>