maven 安装体验及配置部署

maven 安装体验及配置部署

【摘要】Apache Maven是一款项目管理和自动化构建工具,基于项目对象模型(POM)的概念,可以通过一小段描述信息来管理项目的构建,报告和文档的软件项目管理工具,本文主要是 maven 的安装体验及一些配置的简单介绍。

官方地址:http://maven.apache.org/

安装:
wget “http://mirrors.cnnic.cn/apache/maven/maven-3/3.2.2/binaries/apache-maven-3.2.2-bin.tar.gz”

maven运行要依赖Java jdk

JAVA_HOME=/usr/java/jdk
PATH=$JAVA_HOME/bin:/usr/local/maven/bin:$PATH
CLASSPATH=$JAVA_HOME/lib:$JAVA_HOME/jre/lib
export JAVA_HOME CLASSPATH PATH

以下参考:oracle文档

mkdir -p /home/sudops/myproj/

cd /home/sudops/myproj/helloworld

mvn archetype:generate -DgroupId=com.sudops.helloworld -DartifactId=helloworld -Dpackage=com.sudops.helloworld -Dversion=1.0-SNAPSHOT

artifactId: helloworld
version: 1.0-SNAPSHOT
package: com.sudops.helloworld
 Y: :
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:1.1
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.sudops.helloworld
[INFO] Parameter: packageName, Value: com.sudops.helloworld
[INFO] Parameter: package, Value: com.sudops.helloworld
[INFO] Parameter: artifactId, Value: helloworld
[INFO] Parameter: basedir, Value: /home/sudops/myproj
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: /home/sudops/myproj/helloworld
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 02:26 min
[INFO] Finished at: 2014-07-19T22:48:25+08:00
[INFO] Final Memory: 15M/119M
[INFO] ------------------------------------------------------------------------


mvn package
#会download 很多包
[INFO] Scanning for projects...
[INFO]                                                                        
[INFO] ------------------------------------------------------------------------
[INFO] Building helloworld 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/plugins/maven-resources-plugin/2.6/maven-resources-plugin-2.6.pom
。。。。
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.sudops.helloworld.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.032 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ helloworld ---
Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/maven-archiver/2.5/maven-archiver-2.5.pom
Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/maven-archiver/2.5/maven-archiver-2.5.pom (5 KB at 13.1 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-archiver/2.1/plexus-archiver-2.1.pom
Downloaded: http://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-archiver/2.1/plexus-archiver-2.1.pom (3 KB at 8.2 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-io/2.0.2/plexus-io-2.0.2.pom
Downloaded: http://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-io/2.0.2/plexus-io-2.0.2.pom (2 KB at 5.0 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-components/1.1.19/plexus-components-1.1.19.pom
Downloaded: http://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-components/1.1.19/plexus-components-1.1.19.pom (3 KB at 7.7 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus/3.0.1/plexus-3.0.1.pom
Downloaded: http://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus/3.0.1/plexus-3.0.1.pom (19 KB at 53.6 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-interpolation/1.15/plexus-interpolation-1.15.pom
Downloaded: http://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-interpolation/1.15/plexus-interpolation-1.15.pom (1018 B at 3.0 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/commons-lang/commons-lang/2.1/commons-lang-2.1.pom
Downloaded: http://repo.maven.apache.org/maven2/commons-lang/commons-lang/2.1/commons-lang-2.1.pom (10 KB at 28.7 KB/sec)
Downloading: http://repo.maven.apache.org/maven2/org/apache/maven/maven-archiver/2.5/maven-archiver-2.5.jar
Downloading: http://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-interpolation/1.15/plexus-interpolation-1.15.jar
Downloading: http://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-archiver/2.1/plexus-archiver-2.1.jar
Downloading: http://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-io/2.0.2/plexus-io-2.0.2.jar
Downloading: http://repo.maven.apache.org/maven2/commons-lang/commons-lang/2.1/commons-lang-2.1.jar
Downloaded: http://repo.maven.apache.org/maven2/org/apache/maven/maven-archiver/2.5/maven-archiver-2.5.jar (22 KB at 42.1 KB/sec)
Downloaded: http://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-io/2.0.2/plexus-io-2.0.2.jar (57 KB at 106.0 KB/sec)
Downloaded: http://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-interpolation/1.15/plexus-interpolation-1.15.jar (60 KB at 78.4 KB/sec)
Downloaded: http://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-archiver/2.1/plexus-archiver-2.1.jar (181 KB at 179.0 KB/sec)
Downloaded: http://repo.maven.apache.org/maven2/commons-lang/commons-lang/2.1/commons-lang-2.1.jar (203 KB at 168.3 KB/sec)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 01:07 min
[INFO] Finished at: 2014-07-21T10:01:12+08:00
[INFO] Final Memory: 11M/119M
[INFO] ————————————————————————————————————

java -cp target/helloworld-1.0-SNAPSHOT.jar com.sudops.helloworld.App
运行:
 java -cp target/helloworld-1.0-SNAPSHOT.jar com.sudops.helloworld.App
Hello World!

#配置maven连接远程服务器的用户和密码:

cat /usr/local/maven/conf/settings.xml

    <server>
        <id>sudops-maven-repository-snapshots</id>
        <username>root</username>
        <password>myrootpwd</password>
    </server>
    <server>
        <id>sudops-maven-repository</id>
        <username>root</username>
        <password>myrootpwd</password>
    </server>
<!--
    <server>
      <id>sudops-maven-repository-snapshots</id>
      <privateKey>/root/.ssh/id_rsa</privateKey>
      <passphrase></passphrase>
    </server>
    <server>
      <id>sudops-maven-repository</id>
      <privateKey>/root/.ssh/id_rsa</privateKey>
      <passphrase></passphrase>
    </server>
-->

下面是遇到的一些问题,第二次执行mvn deploy的时候会hung住。

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.sudops.helloworld.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.003 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ helloworld ---
[INFO] Building jar: /home/sudops/myproj/helloworld/target/helloworld-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ helloworld ---
[INFO] Installing /home/sudops/myproj/helloworld/target/helloworld-1.0-SNAPSHOT.jar to /dq/maven/com/sudops/helloworld/helloworld/1.0-SNAPSHOT/helloworld-1.0-SNAPSHOT.jar
[INFO] Installing /home/sudops/myproj/helloworld/pom.xml to /dq/maven/com/sudops/helloworld/helloworld/1.0-SNAPSHOT/helloworld-1.0-SNAPSHOT.pom
[INFO]
[INFO] --- maven-deploy-plugin:2.8.1:deploy (default-deploy) @ helloworld ---
Downloading: scp://mvn.sudops.com/proj/maven/snapshots/com/sudops/helloworld/helloworld/1.0-SNAPSHOT/maven-metadata.xml
779/778 B 

第二次运行的时候发现这里会卡住

mvn -X debug显示是通过root用户和密码认证的,改成sshkey方式仍然不起作用,google了半天,改成sftp,终于可以用了。

以下是修改为sftp之后的运行情况:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.sudops.helloworld.AppTest
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.002 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ helloworld ---
[INFO] Building jar: /home/sudops/myproj/helloworld/target/helloworld-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ helloworld ---
[INFO] Installing /home/sudops/myproj/helloworld/target/helloworld-1.0-SNAPSHOT.jar to /dq/maven/com/sudops/helloworld/helloworld/1.0-SNAPSHOT/helloworld-1.0-SNAPSHOT.jar
[INFO] Installing /home/sudops/myproj/helloworld/pom.xml to /dq/maven/com/sudops/helloworld/helloworld/1.0-SNAPSHOT/helloworld-1.0-SNAPSHOT.pom
[INFO]
[INFO] --- maven-deploy-plugin:2.8.1:deploy (default-deploy) @ helloworld ---
Downloading: sftp://mvn.sudops.com/proj/maven/snapshots/com/sudops/helloworld/helloworld/1.0-SNAPSHOT/maven-metadata.xml
Downloaded: sftp://mvn.sudops.com/proj/maven/snapshots/com/sudops/helloworld/helloworld/1.0-SNAPSHOT/maven-metadata.xml (778 B at 3.3 KB/sec)
Uploading: sftp://mvn.sudops.com/proj/maven/snapshots/com/sudops/helloworld/helloworld/1.0-SNAPSHOT/helloworld-1.0-20140721.000800-4.jar
Uploaded: sftp://mvn.sudops.com/proj/maven/snapshots/com/sudops/helloworld/helloworld/1.0-SNAPSHOT/helloworld-1.0-20140721.000800-4.jar (3 KB at 89.6 KB/sec)
Uploading: sftp://mvn.sudops.com/proj/maven/snapshots/com/sudops/helloworld/helloworld/1.0-SNAPSHOT/helloworld-1.0-20140721.000800-4.pom
Uploaded: sftp://mvn.sudops.com/proj/maven/snapshots/com/sudops/helloworld/helloworld/1.0-SNAPSHOT/helloworld-1.0-20140721.000800-4.pom (3 KB at 103.1 KB/sec)
Downloading: sftp://mvn.sudops.com/proj/maven/snapshots/com/sudops/helloworld/helloworld/maven-metadata.xml
Downloaded: sftp://mvn.sudops.com/proj/maven/snapshots/com/sudops/helloworld/helloworld/maven-metadata.xml (292 B at 40.7 KB/sec)
Uploading: sftp://mvn.sudops.com/proj/maven/snapshots/com/sudops/helloworld/helloworld/1.0-SNAPSHOT/maven-metadata.xml
Uploaded: sftp://mvn.sudops.com/proj/maven/snapshots/com/sudops/helloworld/helloworld/1.0-SNAPSHOT/maven-metadata.xml (778 B at 40.0 KB/sec)
Uploading: sftp://mvn.sudops.com/proj/maven/snapshots/com/sudops/helloworld/helloworld/maven-metadata.xml
Uploaded: sftp://mvn.sudops.com/proj/maven/snapshots/com/sudops/helloworld/helloworld/maven-metadata.xml (292 B at 16.8 KB/sec)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.333 s
[INFO] Finished at: 2014-07-21T08:08:00+08:00
[INFO] Final Memory: 13M/148M
[INFO] ------------------------------------------------------------------------    

当前的pom.xml配置情况

# cat 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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.sudops.helloworld</groupId>
  <artifactId>helloworld</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>helloworld</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
     <build>
          <plugins>
               <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.1</version>
                    <configuration>
                         <target>1.6</target>
                         <source>1.6</source>
                         <encoding>utf-8</encoding>
                    </configuration>
               </plugin>

               <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-resources-plugin</artifactId>
                <version>2.6</version>
                    <configuration>
                         <encoding>UTF-8</encoding>
                    </configuration>
               </plugin>
               <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-javadoc-plugin</artifactId>
                <version>2.9.1</version>
                    <configuration>
                         <encoding>UTF-8</encoding>
                         <charset> UTF-8</charset>
                    </configuration>
               </plugin>
               <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.1</version>
               </plugin>
          </plugins>
          <extensions>
               <extension>
                    <groupId>org.apache.maven.wagon</groupId>
                    <artifactId>wagon-ssh</artifactId>
                    <version>2.5</version>
               </extension>
          </extensions>
     </build>
     <distributionManagement>
          <repository>
               <id>duoqu-maven-repository</id>
               <url>sftp://mvn.sudops.com/proj/maven</url>
          </repository>
          <snapshotRepository>
               <id>duoqu-maven-repository-snapshots</id>
               <url>sftp://mvn.sudops.com/proj/maven/snapshots</url>
          </snapshotRepository>
     </distributionManagement>
</project>