maven 基础

一、maven-shade-plugin

maven 工程打包,包括工程依赖及对依赖包的重命名。

如下:打包并配置MainClass

<build>  
  <plugins>  
      <plugin>  
          <groupId>org.apache.maven.plugins</groupId>  
          <artifactId>maven-shade-plugin</artifactId>  
          <version>1.4</version>  
          <executions>  
              <execution>  
                  <phase>package</phase>  
                 <goals>  
                     <goal>shade</goal>  
                 </goals>  
                 <configuration>  
                     <filters>  
                         <filter>  
                             <artifact>*:*</artifact>  
                             <excludes>  
                                 <exclude>META-INF/config.temp</exclude>  
                             </excludes>  
                         </filter>  
                     </filters>  
                     <transformers>  
                         <transformer  
                             implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">  
                             <mainClass>com.***.***</mainClass>  
                         </transformer>  
                         <transformer  
                             implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">  
                             <resource>META-INF/spring.handlers</resource>  
                         </transformer>  
                         <transformer  
                             implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">  
                             <resource>META-INF/spring.schemas</resource>  
                         </transformer>  
                     </transformers>  
                 </configuration>  
             </execution>  
         </executions>  
     </plugin>  
 </plugins>

二、Maven Scope

Dependency Scope 依赖范围:

* compile,缺省值,适用于所有阶段,会随着项目一起发布。 

* provided,类似compile,期望JDK、容器或使用者会提供这个依赖。如servlet.jar。 

* runtime,只在运行时使用,如JDBC驱动,适用运行和测试阶段。 

* test,只在测试时使用,用于编译和运行测试代码。不会随项目发布。 

* system,类似provided,需要显式提供包含依赖的jar,Maven不会在Repository中查找它。

1、compile (编译范围)

compile是默认的范围;如果没有提供一个范围,那该依赖的范围就是编译范围。编译范围依赖在所有的classpath 中可用,同时它们也会被打包。

2、provided (已提供范围)

provided 依赖只有在当JDK 或者一个容器已提供该依赖之后才使用。例如, 如果你开发了一个web 应用,你可能在编译 classpath 中需要可用的Servlet API 来编译一个servlet,但是你不会想要在打包好的WAR 中包含这个Servlet API;这个Servlet API JAR 由你的应用服务器或者servlet 容器提供。已提供范围的依赖在编译classpath (不是运行时)可用。它们不是传递性的,也不会被打包。

3、runtime (运行时范围)

runtime 依赖在运行和测试系统的时候需要,但在编译的时候不需要。比如,你可能在编译的时候只需要JDBC API JAR,而只有在运行的时候才需要JDBC
驱动实现。

4、test (测试范围)

test范围依赖 在一般的编译和运行时都不需要,它们只有在测试编译和测试运行阶段可用。

5、system (系统范围)

system范围依赖与provided 类似,但是你必须显式的提供一个对于本地系统中JAR 文件的路径。这么做是为了允许基于本地对象编译,而这些对象是系统类库的一部分。这样的构件应该是一直可用的,Maven 也不会在仓库中去寻找它。如果你将一个依赖范围设置成系统范围,你必须同时提供一个 systemPath 元素。注意该范围是不推荐使用的(你应该一直尽量去从公共或定制的 Maven 仓库中引用依赖)。

三、pom.xml 样例

<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://maven.apache.org/POM/4.0.0"
         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>**</groupId>
    <artifactId>**</artifactId>
    <packaging>pom</packaging>
    <version>1.0</version>

    <modules>
        <module>**</module>
        <module>**</module>
    </modules>

    <description>**</description>

    <properties>
        <project.build.sourceEncoding>utf-8</project.build.sourceEncoding>
        <jdk.version>1.8</jdk.version>
        <slf4j.api.version>1.7.14</slf4j.api.version>
        <log4j.slf4j.impl.version>2.5</log4j.slf4j.impl.version>
        <log4j.core.version>2.5</log4j.core.version>
        <junit.version>4.12</junit.version>
    </properties>

    <build>
        <pluginManagement>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>${jdk.version}</source>
                        <target>${jdk.version}</target>
                        <compilerVersion>${jdk.version}</compilerVersion>
                    </configuration>
                </plugin>

                <plugin>
                    <groupId>org.codehaus.mojo</groupId>
                    <artifactId>sonar-maven-plugin</artifactId>
                    <version>3.0.1</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

    <profiles>
        <profile>
            <id>test</id>
            <properties>
                <build.profile>test</build.profile>
                <host.nexus.central>
                    http://host:port/nexus/content/repositories/central
                </host.nexus.central>
                <host.nexus.release>
                    http://host:port/nexus/content/repositories/releases
                </host.nexus.release>
                <host.nexus.public>
                    http://host:port/nexus/content/repositories/public
                </host.nexus.public>
                <host.nexus.snapshots>
                    http://host:port/nexus/content/repositories/snapshots
                </host.nexus.snapshots>
            </properties>
            <build>
                <filters>
                    <filter>src/profiles/${build.profile}/config.properties</filter>
                </filters>
                <resources>
                    <resource>
                        <directory>src/main/resources</directory>
                        <filtering>true</filtering>
                    </resource>
                </resources>
            </build>
        </profile>

        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <build.profile>dev</build.profile>
                <host.nexus.central>
                    http://host:port/nexus/content/repositories/central
                </host.nexus.central>
                <host.nexus.release>
                    http://host:port/nexus/content/repositories/releases
                </host.nexus.release>
                <host.nexus.public>
                    http://host:port/nexus/content/repositories/public
                </host.nexus.public>
                <host.nexus.snapshots>
                    http://host:port/nexus/content/repositories/snapshots
                </host.nexus.snapshots>
            </properties>
            <build>
                <filters>
                    <filter>src/profiles/${build.profile}/config.properties</filter>
                </filters>
                <resources>
                    <resource>
                        <directory>src/main/resources</directory>
                        <filtering>true</filtering>
                    </resource>
                </resources>
            </build>
        </profile>

        <profile>
            <id>prod</id>
            <properties>
                <build.profile>prod</build.profile>
                <host.nexus.central>
                    http://host:port/nexus/content/repositories/central
                </host.nexus.central>
                <host.nexus.release>
                    http://host:port/nexus/content/repositories/releases
                </host.nexus.release>
                <host.nexus.public>
                    http://host:port/nexus/content/repositories/public
                </host.nexus.public>
                <host.nexus.snapshots>
                    http://host:port/nexus/content/repositories/snapshots
                </host.nexus.snapshots>
            </properties>
            <build>
                <filters>
                    <filter>src/profiles/${build.profile}/config.properties</filter>
                </filters>
                <resources>
                    <resource>
                        <directory>src/main/resources</directory>
                        <filtering>true</filtering>
                    </resource>
                </resources>
            </build>
        </profile>
    </profiles>

    <repositories>
        <repository>
            <id>nexus-central</id>
            <name>nexus central</name>
            <url>${host.nexus.central}</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>

        <repository>
            <id>nexus-release</id>
            <name>nexus release</name>
            <url>${host.nexus.release}</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>

        <repository>
            <id>nexus-snapshots</id>
            <name>nexus snapshots</name>
            <url>${host.nexus.snapshots}</url>
            <releases>
                <enabled>false</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j.api.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
            <version>${log4j.slf4j.impl.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>${log4j.core.version}</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

内置属性(Maven预定义可以直接使用)

${basedir} 项目根目录 
${version}表示项目版本;
${project.basedir}同${basedir};
${project.version}表示项目版本,与${version}相同;
${project.build.directory} 构建目录,缺省为target
${project.build.sourceEncoding}表示主源码的编码格式;
${project.build.sourceDirectory}表示主源码路径;
${project.build.finalName}表示输出文件名称;
${project.build.outputDirectory} 构建过程输出目录,缺省为target/classes

 

posted @ 2016-10-20 09:51  WindWant  阅读(550)  评论(0编辑  收藏  举报
文章精选列表