Maven 使用笔记

一:手动添加包进本地仓库

  Maven 安装 JAR 包的命令是:

mvn install:install-file -Dfile=jar包的位置 -DgroupId=【groupId】 -DartifactId=【artifactId】 -Dversion=【version】 -Dpackaging=jar


  例如我下了一个IK分词器想用于项目中,可以自行安装到本地库中。不知道groupId可以点开jar包看,至于artifactId 和 version 就自己填了。只要在pom中一致即可

  mvn install:install-file -Dfile=/home/hanson/IKAnalyzer.jar -DgroupId=org.wltea.analyzer -DartifactId=IKAnalyzer -Dversion=2012FF_u1 -Dpackaging=jar

 

  自己的maven项目什么的可以直接 mvn install

  下面也是一些常用包的下载仓库。

  http://mvnrepository.com/
  http://search.maven.org/
  http://repository.sonatype.org/content/groups/public/
  http://people.apache.org/repo/m2-snapshot-repository/
  http://people.apache.org/repo/m2-incubating-repository/

二:用assembly 插件 把一组文件、目录、依赖元素组装成一个归档文件。

  插件网址 http://maven.apache.org/plugins/maven-assembly-plugin/ 

  版本号可在 http://maven.apache.org/plugins/index.html 查看

  举个例子,如果的你的Maven 2工程包含”src/main/bin”这个目录,你可以指示Assembly插件复制“src/main/bin”目录下所有的文件到bin目录里(归档文件里的目录),并且可以修改它们的权限属性(UNIX mode)

 

  需要在pom.xml文件里添加 maven-assembly-plugin 插件

<plugin>

    <artifactId>maven-assembly-plugin</artifactId>

    <executions>  <!--执行器 mvn assembly:assembly-->

        <execution>

            <id>make-zip</id><!--名字任意 -->  

        <phase>package</phase><!-- 绑定到package生命周期阶段上 -->  

        <goals>  

           <goal>single</goal><!-- 只运行一次 -->  

        </goals>  

            <configuration>

                     <descriptors> <!--描述文件路径-->

                          <descriptor>src/main/resources/zip.xml</descriptor>

                    </descriptors>

            </configuration>

        </execution>

    </executions>

 </plugin>

描述文件设置如下

<assembly

    xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">

    <id>release</id>

    <formats>

        <format>zip</format>

    </formats>

    <fileSets>

        <fileSet>

            <directory>${project.basedir}\src\main\config</directory>

            <!-- 过滤 -->

            <excludes>

                <exclude>*.xml</exclude>

            </excludes>

            <outputDirectory>\</outputDirectory>

        </fileSet>

    </fileSets>

    

    <dependencySets>

        <dependencySet>

            <useProjectArtifact>true</useProjectArtifact>

            <outputDirectory>lib</outputDirectory><!-- 将scope为runtime的依赖包打包到lib目录下。 -->

            <scope>runtime</scope>

        </dependencySet>

    </dependencySets>

</assembly>

此描述文件中 使用以下格式

打包的文件格式

可以有:tar.zip war zip

 <formats>

  <format>zip</format>

 </formats>



 



需要打包的路径

<directory>${project.basedir}</directory>



 



打包后输出的路径

<outputDirectory>/</outputDirectory>



 



打包需要包含的文件



 <excludes>

         <exclude>junit:junit</exclude>

         <exclude>commons-lang:commons-lang</exclude>

         <exclude>commons-logging:commons-logging</exclude>

 </excludes>



 



当前项目构件是否包含在这个依赖集合里。



<useProjectArtifact>true</useProjectArtifact>



 



依赖包打包到目录下

<dependencySets>

   <dependencySet>

    <outputDirectory>lib</outputDirectory><!-- 将scope为runtime的依赖包打包到lib目录下。 -->

    <useProjectArtifact>true</useProjectArtifact>

    <scope>runtime</scope>

   </dependencySet>

 </dependencySets>

 

三、编译时跳过测试 

-DskipTests 编译测试用例

-Dmaven.test.skip=true  不编译测试用例

 

四、下载源代码

 

进入相应的pom.xml目录 

键入 

mvn dependency:sources mvn dependency:resolve -Dclassifier=javadoc

 

或在.m2的 setting.xml里添加

<profiles>

<profile>

    <id>downloadSources</id>

    <properties>

        <downloadSources>true</downloadSources>

        <downloadJavadocs>true</downloadJavadocs>           

    </properties>

</profile>

</profiles>



<activeProfiles>

  <activeProfile>downloadSources</activeProfile>

</activeProfiles>

 

你可能感兴趣的:(maven)