rogerxu / rogerxu.github.io

Roger Xu's Blog
3 stars 2 forks source link

Maven #130

Open rogerxu opened 7 years ago

rogerxu commented 7 years ago

Maven – Welcome to Apache Maven

rogerxu commented 7 years ago

test-resources

rogerxu commented 7 years ago

war plugin

Include/Exclude resources

include when copy web resources exclude when copy web resources exclude when packaging

web.xml

Replace web.xml

<webXml>src/test/resources/config/web.xml</webXml>
rogerxu commented 7 years ago

Surefire Plugin

Maven Surefire Plugin – Introduction

skip tests

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <skipTests>true</skipTests>
    </configuration>
</plugin>

TestNG

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.20</version>
    <configuration>
        <suiteXmlFiles>
            <suiteXmlFile>${project.build.testOutputDirectory}/testng.xml</suiteXmlFile>
        </suiteXmlFiles>
    </configuration>
</plugin>
rogerxu commented 7 years ago

Dependency

Maven – Introduction to the Dependency Mechanism

scope

Dependency Management

A very important use of the dependency management section is to control the versions of artifacts used in transitive dependencies.

Importing Dependencies

In larger projects it may be impossible to accomplish this since a project can only inherit from a single parent. To accommodate this, projects can import managed dependencies from other projects. This is accomplished by declaring a pom artifact as a dependency with a scope of "import".

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

download dependency

extract dependency

Filter dependency

Maven – Optional Dependencies and Dependency Exclusions

Apache Maven Dependency Plugin – Resolving conflicts using the dependency tree

Apache Maven Dependency Plugin – dependency:tree

$ mvn dependency:tree -Dincludes=:bcpkix-jdk15on
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-security</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.bouncycastle</groupId>
            <artifactId>bcpkix-jdk15on</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<dependency>
    <artifactId>bcpkix-jdk15on</artifactId>
    <groupId>org.bouncycastle</groupId>
    <version>1.60</version>
    <scope>test</scope>
</dependency>
rogerxu commented 7 years ago

cargo plugin

deploy war packages

deploy expanded war of current project

<plugin>
    <groupId>org.codehaus.cargo</groupId>
    <artifactId>cargo-maven2-plugin</artifactId>
    <executions>
        <execution>
            <id>start-container</id>
            <phase>pre-integration-test</phase>
            <goals>
                <goal>start</goal>
            </goals>
        </execution>
        <execution>
            <id>stop-container</id>
            <phase>post-integration-test</phase>
            <goals>
                <goal>stop</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <skip>${skipBuildPlugins}</skip>
        <container>
            <containerId>jetty9x</containerId>
            <type>embedded</type>
            <timeout>180000</timeout>
            <systemProperties>
                <http.proxyHost>proxy</http.proxyHost>
                <http.proxyPort>8080</http.proxyPort>
                <http.nonProxyHosts>*.corp|localhost</http.nonProxyHosts>

                <org.eclipse.jetty.xml.XmlParser.NotValidating>true</org.eclipse.jetty.xml.XmlParser.NotValidating>
                <java.io.tmpdir>${java.io.tmpdir}</java.io.tmpdir>
            </systemProperties>
            <dependencies>
            </dependencies>
        </container>
        <configuration>
            <properties>
                <cargo.servlet.port>${jetty.dyn.port}</cargo.servlet.port>
            </properties>
        </configuration>
        <deployables>
            <deployable>
                <location>${project.build.directory}/${project.build.finalName}</location>
                <pingURL>http://localhost:${jetty.dyn.port}/${web.context.name}/index.html</pingURL>
                <pingTimeout>3000</pingTimeout>
            </deployable>
        </deployables>
    </configuration>
</plugin>

dev mode?

dependencies for container

rogerxu commented 7 years ago

jetty plugin

Dev mode

$ mvn jetty:run
<plugin>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    <version>${jetty.version}</version>
    <configuration>
        <httpConnector>
            <port>${jetty.port}</port>
        </httpConnector>
        <scanIntervalSeconds>10</scanIntervalSeconds>
        <stopPort>${jetty.stopPort}</stopPort>
        <stopKey>foo</stopKey>
        <webAppSourceDirectory>${webapp.src.dir}</webAppSourceDirectory>
        <webApp>
            <contextPath>/${web.context.name}</contextPath>
            <defaultsDescriptor>${jetty.config.path}/webdefault.xml</defaultsDescriptor>
            <descriptor>${webapp.src.dir}/WEB-INF/web.xml</descriptor>
        </webApp>
        <contextHandlers>
            <!-- UI library -->
            <contextHandler implementation="org.eclipse.jetty.maven.plugin.JettyWebAppContext">
                <war>${project.build.directory}/wars/ui-lib.war</war>
                <contextPath>/uilib</contextPath>
            </contextHandler>
        </contextHandlers>
    </configuration>
</plugin>

Reserve port

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>qunit-reserve-network-port</id>
            <goals>
                <goal>reserve-network-port</goal>
            </goals>
            <phase>process-resources</phase>
            <configuration>
                <portNames>
                    <portName>jetty.dyn.port</portName>
                </portNames>
            </configuration>
        </execution>
    </executions>
</plugin>
rogerxu commented 7 years ago

Lifecycle

Maven – Introduction to the Build Lifecycle

rogerxu commented 7 years ago

antrun plugin

echo message

<!-- System Environment -->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-antrun-plugin</artifactId>
    <executions>
        <execution>
            <id>system-info</id>
            <phase>initialize</phase>
            <goals>
                <goal>run</goal>
            </goals>
            <configuration>
                <target>
                    <echo>shell.ext=${shell.ext}</echo>
                    <echo>arch.ext=${arch.ext}</echo>
                </target>
            </configuration>
        </execution>
    </executions>
</plugin>
rogerxu commented 7 years ago

Profiles

Maven – Introduction to build profiles

$ mvn clean verify -P profile1,profile2

Activation

Active by default

Profiles can also be active by default using a configuration like the following:

<profiles>
  <profile>
    <id>profile-1</id>
    <activation>
      <activeByDefault>true</activeByDefault>
    </activation>
    ...
  </profile>
</profiles>

This profile will automatically be active for all builds unless another profile in the same POM is activated using one of the previously described methods. All profiles that are active by default are automatically deactivated when a profile in the POM is activated on the command line or through its activation config.

Active by property

The following profile will be activated when the system property "debug" is not defined at all:

<profiles>
  <profile>
    <activation>
      <property>
        <name>!debug</name>
      </property>
    </activation>
    ...
  </profile>
</profiles>

The next example will trigger the profile when the system property "environment" is specified with the value "test":

<profiles>
  <profile>
    <activation>
      <property>
        <name>environment</name>
        <value>test</value>
      </property>
    </activation>
    ...
  </profile>
</profiles>

Active by OS-specified

Apache Maven Enforcer Rules – Require OS Version

<!-- Widnows -->
<profile>
    <id>windows</id>
    <activation>
        <os>
            <family>windows</family>
        </os>
    </activation>
    <properties>
        <shell.ext>bat</shell.ext>
        <arch.ext>zip</arch.ext>
    </properties>
</profile>

<!-- Unix -->
<profile>
    <id>unix</id>
    <activation>
        <os>
            <family>unix</family>
        </os>
    </activation>
    <properties>
        <shell.ext>sh</shell.ext>
        <arch.ext>tar.bz2</arch.ext>
    </properties>
</profile>

<!-- Mac -->
<profile>
    <id>mac</id>
    <activation>
        <os>
            <family>mac</family>
        </os>
    </activation>
    <properties>
        <shell.ext>sh</shell.ext>
        <arch.ext>zip</arch.ext>
    </properties>
</profile>
rogerxu commented 7 years ago

settings.xml

Maven – Settings Reference

settings.xml

There are two locations where a settings.xml file may live:

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">

    <proxies>
        <proxy>
            <id>proxy</id>
            <active>true</active>
            <protocol>http</protocol>
            <host>proxy</host>
            <port>8080</port>
            <nonProxyHosts>*.corp|nexus</nonProxyHosts>
        </proxy>
    </proxies>

    <mirrors>
        <mirror>
            <id>nexus-releases</id>
            <url>https://oss.sonatype.org/content/repositories/releases/</url>
            <mirrorOf>central</mirrorOf>
        </mirror>
        <mirror>
            <id>nexus-snapshots</id>
            <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
            <mirrorOf>*,!central</mirrorOf>
        </mirror>
    </mirrors>

    <profiles>
        <profile>
            <id>release.build</id>
            <pluginRepositories>
                <pluginRepository>
                    <id>central</id>
                    <url>https://oss.sonatype.org/content/repositories/releases/</url>
                </pluginRepository>
            </pluginRepositories>
            <repositories>
                <repository>
                    <id>central</id>
                    <url>https://oss.sonatype.org/content/repositories/releases/</url>
                </repository>
            </repositories>
            <properties>
                <tycho.disableP2Mirrors>true</tycho.disableP2Mirrors>
                <tycho.localArtifacts>ignore</tycho.localArtifacts>
            </properties>
        </profile>
        <profile>
            <id>snapshot.build</id>
            <pluginRepositories>
                <pluginRepository>
                    <id>central</id>
                    <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
                </pluginRepository>
            </pluginRepositories>
            <repositories>
                <repository>
                    <id>central</id>
                    <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
                </repository>
            </repositories>
            <properties>
                <tycho.disableP2Mirrors>true</tycho.disableP2Mirrors>
                <tycho.localArtifacts>ignore</tycho.localArtifacts>
            </properties>
        </profile>
        <profile>
            <id>sonar</id>
                <activation>
                    <activeByDefault>false</activeByDefault>
                </activation>
                <properties>
                    <tycho.disableP2Mirrors>true</tycho.disableP2Mirrors>
                    <tycho.localArtifacts>ignore</tycho.localArtifacts>
                </properties>
        </profile>
    </profiles>

    <activeProfiles>
        <activeProfile>release.build</activeProfile>
    </activeProfiles>
</settings>

Repositories

Mirrors

.mvn

Root module

maven.config

-s settings.xml
-DproxySet=true
-DproxyHost=localhost
-DproxyPort=8080
-DskipTests
rogerxu commented 7 years ago

Run Node.js

eirslett/frontend-maven-plugin: "Maven-node-grunt-gulp-npm-node-plugin to end all maven-node-grunt-gulp-npm-plugins." A Maven plugin that downloads/installs Node and NPM locally, runs NPM install, Grunt, Gulp and/or Karma.

rogerxu commented 7 years ago

exec plugin

exec

TODO

java

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>install-phantomjs</id>
            <phase>pre-integration-test</phase>
            <goals>
                <goal>java</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <mainClass>com.example.demo.PhantomJSInstaller</mainClass>
        <classpathScope>test</classpathScope>
        <systemProperties>
            <systemProperty>
                <key>java.io.tmpdir</key>
                <value>${java.io.tmpdir}</value>
            </systemProperty>
        </systemProperties>
    </configuration>
</plugin>
rogerxu commented 7 years ago

Batch Mode

Disable download progress in Jenkins console log.

https://stackoverflow.com/questions/21638697/disable-maven-download-progress-indication/35653426

$ mvn -B ...

http://blog.sonatype.com/2009/01/maven-continuous-integration-best-practices/

4 Enable Batch Mode Tip: Enable -B (batch) mode on the build. This will make the logs shorter since it avoids the dependency download progress logging. It also ensures that the build won't hang due to waiting for user input. (to enable globally in settings.xml: <interactiveMode>false</interactiveMode>)

rogerxu commented 7 years ago

Versions Plugin

Versions Maven Plugin – Introduction

Display dependency updates

$ mvn versions:display-dependency-updates

Display plugin updates

$ mvn versions:display-plugin-updates
rogerxu commented 6 years ago

Help Plugin

Maven Help Plugin - Introduction

effective pom

$ mvn help:effective-pom
<plugin>
    <artifactId>maven-help-plugin</artifactId>
    <version>2.2</version>
    <executions>
        <execution>
            <id>effective-pom</id>
            <phase>initialize</phase>
            <configuration>
            </configuration>
            <goals>
                <goal>effective-settings</goal>
                <goal>effective-pom</goal>
            </goals>
        </execution>
    </executions>
</plugin>
rogerxu commented 6 years ago

Failsafe Plugin

Maven Failsafe Plugin – Introduction

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.20.1</version>
    <executions>
        <execution>
            <goals>
                <goal>integration-test</goal>
                <goal>verify</goal>
            </goals>
        </execution>
    </executions>
</plugin>
rogerxu commented 6 years ago

Artifact

Classifier

Attach artifacts