apache-maven-3.0.4

apache-maven-3.0.4

HOW TO ...

how to setting locale repository

configure apache-maven-3.0.4\conf\settings.xml

  <!-- localRepository
   | The path to the local repository maven will use to store artifacts.
   |
   | Default: ~/.m2/repository
  <localRepository>/path/to/local/repo</localRepository>
  -->
  <localRepository>C:/maven-repository</localRepository>

copy the "apache-maven-3.0.4\conf\settings.xml" file to new locale repository "C:\maven-repository".

Then maven will update locale repository from remote Central Repository.

The lib download from remote Central Repository will store in C:\maven-repository.

how to change remote Central Repository address

configure pom.xml in your project like this.

    <repositories>
        <repository>
            <id>central</id>
            <name>Central Repository</name>
            <url>https://nexus.sourcesense.com/nexus/content/repositories/public/</url>
            <layout>default</layout>
            <!-- open snapshot -->
            <snapshots>
                <enabled>true</enabled>
            </snapshots>
            <!-- open release -->
            <releases>
                <enabled>true</enabled>
            </releases>
        </repository>
    </repositories>

Then new Central Repository address "https://nexus.sourcesense.com/nexus/content/repositories/public/" will replace the dafault Central Repository address configure in "apache-maven-3.0.4\lib\maven-model-builder-3.0.4.jar\org\apache\maven\model\pom-4.0.0.xml".

  <repositories>
    <repository>
      <id>central</id>
      <name>Central Repository</name>
      <url>http://repo.maven.apache.org/maven2</url>
      <layout>default</layout>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
  </repositories>

http://repo.maven.apache.org/maven2 is the default Central Repository address.

And another way to change central repository configure in "apache-maven-3.0.4\lib\maven-model-builder-3.0.4.jar\org\apache\maven\model\pom-4.0.0.xml" is to configure your locale repository (C:\maven-repository\settings.xml) like this.

add profile configure to profiles elements and active it.

    <profile>
      <id>central-repos</id>
      <repositories>
        <repository>
          <id>central</id>
          <name>Central Repository</name>
          <url>https://nexus.sourcesense.com/nexus/content/repositories/public/</url>
          <layout>default</layout>
          <!-- open snapshot -->
          <snapshots>
            <enabled>true</enabled>
          </snapshots>
          <!-- open release -->
          <releases>
            <enabled>true</enabled>
          </releases>
        </repository>
      </repositories>
    </profile>
 
  <!-- activeProfiles
   | List of profiles that are active for all builds.
   |
  <activeProfiles>
    <activeProfile>alwaysActiveProfile</activeProfile>
    <activeProfile>anotherAlwaysActiveProfile</activeProfile>
  </activeProfiles>
  -->
  <activeProfiles>
    <activeProfile>central-repos</activeProfile>
  </activeProfiles>

how to solve dependency conflict

same dependency path length - first come, first use

user-core depends on log4j-1.2.16

user-log depends on log4j-1.29

user-dao depends on user-core and user-log

which log4j should user-dao depends on?

user-core pom.xml

 <groupId>tos.maven.user</groupId>
 <artifactId>user-core</artifactId>
 <version>0.0.1-SNAPSHOT</version>

  <dependency>
   <groupId>log4j</groupId>
   <artifactId>log4j</artifactId>
   <version>1.2.16</version>
  </dependency>

user-log pom.xml

 <groupId>tos.maven.user</groupId>
 <artifactId>user-log</artifactId>
 <version>0.0.1-SNAPSHOT</version> 

   <dependency>
   <groupId>log4j</groupId>
   <artifactId>log4j</artifactId>
   <version>1.2.9</version>
  </dependency>

 user-dao pom.xml
  <dependency>
   <groupId>tos.maven.user</groupId>
   <artifactId>user-core</artifactId>
   <version>0.0.1-SNAPSHOT</version>
  </dependency>

  <dependency>
   <groupId>tos.maven.user</groupId>
   <artifactId>user-log</artifactId>
   <version>0.0.1-SNAPSHOT</version>
  </dependency>

in pom.xml of user-dao which dependency come first, the corresponding log4j will be used, so user-dao will use log4j-1.2.16.

different dependency path length - short come, first use

user-service depends on user-dao and user-dao depends on user-core (which depends on log4j-1.2.16).

user-service depends on user-log (which depends on log4j-1.2.9).

then user-service will use log4j-1.2.9 because the length of dependency path is shorter than another (log4j-1.2.16).

if you want to controll exactly which log4j should be used, you can add exclusions element like this.

  <dependency>
   <groupId>tos.maven.user</groupId>
   <artifactId>user-log</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <exclusions>
    <exclusion>
     <groupId>log4j</groupId>
     <artifactId>log4j</artifactId>
    </exclusion>
   </exclusions>
  </dependency>

then user-service will use log4j-1.2.16.

if you want to use log4j-1.2.9. you can change pom.xml of use-service like this

  <dependency>
   <groupId>tos.maven.user</groupId>
   <artifactId>user-log</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <exclusions>
    <exclusion>
     <groupId>log4j</groupId>
     <artifactId>log4j</artifactId>
    </exclusion>
   </exclusions>
  </dependency>
  <dependency>
   <groupId>tos.maven.user</groupId>
   <artifactId>user-dao</artifactId>
   <version>0.0.1-SNAPSHOT</version>
  </dependency>
  <dependency>
   <groupId>log4j</groupId>
   <artifactId>log4j</artifactId>
   <version>1.2.9</version>
  </dependency>

because log4j-1.2.9 is the most short dependency path for now.

how to aggregation mutil project

aggregation user-core, user-log, user-dao, user-service together.

  <modelVersion>4.0.0</modelVersion>
  <groupId>tos.maven.user</groupId>
  <artifactId>user-aggregation</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>pom</packaging>

    <modules>
        <module>../user-core</module>
        <module>../user-log</module>
        <module>../user-dao</module>
        <module>../user-service</module>
    </modules>

how to extends

user-parent pom.xml

    <groupId>tos.maven.user</groupId>
    <artifactId>user-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>

  <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>..</groupId>
                <artifactId>..</artifactId>
                <version>..</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

user-component pom.xml

    <parent>
        <groupId>tos.maven.user</groupId>
        <artifactId>user-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../user-parent/pom.xml</relativePath>
    </parent>

    <dependencies>
        <dependency>
            <groupId>..</groupId>
            <artifactId>..</artifactId>
        </dependency>
    </dependencies>

how to use plugins

source plugin

jar-no-fork: package java source file

configure source plugin in user-parent pom.xml

the following configuration will binding source plugin goal jar-no-fork to phase package

<build>
  <pluginManagement>
   <plugins>
    <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-source-plugin</artifactId>
     <version>2.2</version>
     <executions>
      <execution>
       <phase>package</phase>
       <goals>
        <goal>jar-no-fork</goal>
       </goals>
      </execution>
     </executions>
    </plugin>
   </plugins>
  </pluginManagement>
 </build>

user-component pom.xml

<build>
  <plugins>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-source-plugin</artifactId>
   </plugin>
  </plugins>
 </build>

help plugin

describe: display help information of special plugin

The plugin parameter is meant to provide two things: convenience and prefix-based access.

The convenience comes when specifying a plugin by groupId:artifactId:version. Where the more traditional specification of separate fields would mean specifying this:

mvn help:describe -DgroupId=org.apache.maven.plugins -DartifactId=maven-compiler-plugin -Dversion=2.5.1

the use of the plugin parameter allows this:

mvn help:describe -Dplugin=org.apache.maven.plugins:maven-compiler-plugin:2.5.1

On the other hand, the plugin parameter also offers the option to specify a plugin by its prefix, like this:

mvn help:describe -Dplugin=compiler

sql plugin

set properties

 <properties>
  <mysql.driver>com.mysql.jdbc.Driver</mysql.driver>
  <mysql.url>jdbc:mysql://localhost:3306/mysql</mysql.url>
  <mysql.username>root</mysql.username>
  <mysql.password>mysqladmin</mysql.password>
 </properties>

configure plugin

   <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>sql-maven-plugin</artifactId>
    <version>1.5</version>
    <dependencies>
     <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.21</version>
     </dependency>
    </dependencies>
    <configuration>
     <driver>${mysql.driver}</driver>
     <url>${mysql.url}</url>
     <username>${mysql.username}</username>
     <password>${mysql.password}</password>
     <sqlCommand>
      create database if not exists maven_sql_plugin
     </sqlCommand>
    </configuration>
    <executions>
     <execution>
      <phase>package</phase>
      <goals>
       <goal>execute</goal>
      </goals>
     </execution>
    </executions>
   </plugin>

how to deploy your project to tomcat

setting scope=provided - compile and test will use those jar, but when package your project the jar will not included.

   <dependency>
    <groupId>servletapi</groupId>
    <artifactId>servletapi</artifactId>
    <version>2.4</version>
    <scope>provided</scope>
   </dependency>

   <dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>jsp-api</artifactId>
    <version>2.2</version>
    <scope>provided</scope>
   </dependency>

And use war-plugin and cargo-plugin to deploy to tomcat.

转载于:https://www.cnblogs.com/toy-soldiers/archive/2012/09/08/2672458.html

猜你喜欢

转载自blog.csdn.net/weixin_33978044/article/details/93664072