Tag Archives: maven

http://maven.apache.org/guides/getting-started/

Maven deploy and release

The “SNAPSHOT” term means that the build is a snapshot of your code at a given time.

It usually means that the version is a version still under heavy development.

When it comes time to release your code, you will want to change the version listed in the pom. So instead of having a “1.0.0-SNAPSHOT” you would have something like “1.0.0″.

If your project.version contains SNAPSHOT (f.e., 1.2-SNAPSHOT) and you execute mvn deploy, artifacts will be deployed to your snapshot repository. If it doesn’t (f.e., 1.2) – they will be deployed to your release repository.

Maven 项目管理的时候,日常发布版本,我们都是直接 mvn deploy 到一个 SNAPSHOT 版本,那如何发布 stable 版本呢?

本篇描述了,如何使用 mvn 命令对一个项目进行从 1.0.0-SNAPSHOT 版本升级到 1.0.0,同时在 scm 中进行打 tag 后,修改版本到 1.0.1-SNAPSHOT,同时 commit 到 branch。

#1. 添加 scm 和 plugin 定义

<scm>
  <developerConnection>scm:git:git@git.n.xiaomi.com:allex/yp-assist.git</developerConnection>
</scm>
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-release-plugin</artifactId>
  <version>2.5</version>
  <!--
  <configuration>
    <tagBase>https://code.miliao.com/repos/xiaomi-miliao/tags/src/cloud/yp-assist/</tagBase>
  </configuration>
  -->
</plugin>

为了保障 svn 目录清洁,建议在使用 svn 作为 scm 时,添加上面注释的段落,git 项目不用加

#2. release:prepare 准备工作

$ mvn release:prepare
  • 忽略 SNAPSHOT 依赖,强制发布,mvn release:prepare -DignoreSnapshots=true – 和 -DskipTest=true 一样令人讨厌,不建议使用
  • 此操作会向 git/svn 写入内容
  • 新手建议:-DdryRun=true 此命令加 dryRun 是演习,不会向 git/svn checkin 任何内容。-DdryRun=true 可用 mvn release:clean 回滚。
  • 会检查各个版本是否是 SNAPSHOT,会在运行中要求你回答使用什么版本。
  • 准备工作的作用:加版本 & checkin + 打 tag 到 git/svn 中

#3. 发包工作

$ mvn release:perform
  • 这一步,会去一个临时目录中,把 commit 的代码抓出来 build 之后 deploy。

#4. 清理工作

$ mvn release:clean

References Pages:

maven pom.xml settings

Adding main class to manifest

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-jar-plugin</artifactId>
  <version>2.4</version>
  <configuration>
    <archive>
      <manifest>
        <mainClass>com.yahoo.platform.yui.compressor.Bootstrap</mainClass>
      </manifest>
    </archive>
  </configuration>
</plugin>

Customize java source directory:

Some project’s source directory is not following the maven convention. Instead of being inside src/main/java

Just add this to your pom in the build section.

<sourceDirectory>
  ${basedir}/src
</sourceDirectory>

Here’s the relevant section of the POM doc on configuring the directories.

Including local JAR files as dependency in a Maven project

To avoid fetch dependency from remote repository is to use Maven’s system scope and systemPath feature:

<dependency>
  <groupId>net.sf</groupId>
  <artifactId>jargs</artifactId>
  <version>1.0</version>
  <scope>system</scope>
  <systemPath>${basedir}/lib/jargs-1.0.jar</systemPath>
</dependency>

This will reference a dependency from the local filesystem, which means you do not have to install the JAR into the repository in order to use it. This is particularly useful when you’re doing some prototyping or research into a new technology.

Skipping Tests

If you want to skip tests by default but want the ability to re-enable tests from the command line, you need to go via a properties section in the pom:

<project>
  [...]
  <properties>
    <skipTests>true</skipTests>
  </properties>
  [...]
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>2.16</version>
        <configuration>
          <skipTests>${skipTests}</skipTests>
        </configuration>
      </plugin>
    </plugins>
  </build>
  [...]
</project>

This will allow you to run with tests disabled by default and to run them with this command:

mvn install -DskipTests=false

If you absolutely must, you can also use the maven.test.skip property to skip compiling the tests. maven.test.skip is honored by Surefire, Failsafe and the Compiler Plugin.

mvn install -Dmaven.test.skip=true

For details see Skipping Tests