安装mvn

1
sudo apt-get install maven2

安装 nexus 搭建 maven2 私服

  • 下载 nexus, 官网地址 http://nexus.sonatype.org/ , 我的360网盘地址: http://yunpan.cn/ccxjgqeQx8qjp, 分享中包括 离线 maven index 文件 后面会用到, 解压 nexus
  • 将indexer-cli-5.1.1.jar nexus-maven-repository-index.gz nexus-maven-repository-index.properties 放在同一目录下,执行
    1
    java -jar indexer-cli-5.1.1.jar -u nexus-maven-repository-index.gz -d indexer

  • 将 indexer 中的所有文件移至 nexus/sonatype-work/nexus/indexer/central-ctx/下

  • 启动 nexus 服务 ./nexus start 即可

  • 浏览器打开 localhost:8081/nexus 如果是终端 localhost 改为 ip地址即可

  • 初始账号 admin:admin123
  • 修改 central 中的 Download Remote Indexes 改为 true

  • 可以自己上传本地 jar,如果从远程下载速度慢,可以先自己下载再上传,下载网址[个人认为该网址比较齐全] http://mvnrepository.com/

修改 /etc/maven2/settings.xml, 修改之前最好 备份 文件, 添加 私服mirrors,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<mirrors>
<!-- 设置 mirror -->
<mirror>
<id>central</id>
<mirrorOf>*</mirrorOf>
<name>Human Readable Name for this Mirror.</name>
<url>http://yourip:8081/nexus/content/groups/public</url>
</mirror>
</mirrors>

<!-- 添加 profile -->
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>nexus</id>
<name>Nexus</name>
<url>http://yourip:8081/nexus/content/groups/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</profile>

<!-- 激活 profie -->
<activeProfiles>
<activeProfile>nexus</activeProfile>
</activeProfiles>

创建项目

  • 使用如下命令

    1
    mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
  • 如果卡在下载 archetype-catalog 则 使用如下命令

    1
    mvn archetype:generate -DarchetypeCatalog=internal -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
  • 修改pom.xml 文件,添加 dependences, plugins 等

  • 相关命令(摘抄自maven 官方文档)
  • validate: validate the project is correct and all necessary information is available
  • compile: compile the source code of the project
  • test: test the compiled source code using a suitable unit testing framework. These tests should not require the code be packaged or deployed
  • package: take the compiled code and package it in its distributable format, such as a JAR.
  • integration-test: process and deploy the package if necessary into an environment where integration tests can be run
  • verify: run any checks to verify the package is valid and meets quality criteria
  • install: install the package into the local repository, for use as a dependency in other projects locally
  • deploy: done in an integration or release environment, copies the final package to the remote repository for sharing with other developers and projects.
  • clean: cleans up artifacts created by prior builds
  • site: generates site documentation for this project

创建 jar 包

  • mvn clean package
  • mvn clean compile
  • mvn clean install

添加 plugins

在 pom.xml 中增加 标签 [在跟标签project下]
如添加 maven-shade-plugin, 主要在jar包写入Manifest信息(用法参考 https://maven.apache.org/plugins/maven-shade-plugin/examples/resource-transformers.html)

1
<build>
  <plugins>
      <plugin>  
          <groupId>org.apache.maven.plugins</groupId>  
          <artifactId>maven-shade-plugin</artifactId>  
          <version>1.2.1</version>  
          <executions>  
              <execution>  
                  <phase>package</phase>  
                  <goals>  
                      <goal>shade</goal>  
                  </goals>  
                  <configuration>  
                      <transformers>  
                          <!-- Setting Manifest Entries --> 
                          <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">            
                              <mainClass>com.mycompany.app.App</mainClass>  
                          </transformer>  
                      </transformers>  
                  </configuration>  
              </execution>  
          </executions>  
      </plugin>  
  </plugins>
</build>

执行 jar

  • java -jar target/my-app-1.0-SNAPSHOT.jar [前提是增加了 maven-shade-plugin]
  • java -cp target/my-app-1.0-SNAPSHOT.jar com.walterlife.common.App com.mycompany.app

更改maven 编译时使用的jdk 版本

  • 在 pom.xml 添加如下配置[节点下]
    1
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
            <source>1.8</source>
            <target>1.8</target>
            <compilerArgument></compilerArgument>
        </configuration>
    </plugin>

参考文章

留言

Jun 12 2015