Maven 构建工具


#Java 笔记


使用更快的仓库

maven 配置阿里云的代理仓库

如果你用 maven 管理java项目,打开maven安装目录的 conf/settings.xml 文件,在<mirrors></mirrors>中添加:

<mirror>
    <id>aliyunmaven</id>
    <mirrorOf>*</mirrorOf>
    <name>阿里云公共仓库</name>
    <url>https://maven.aliyun.com/repository/public</url>
</mirror>

或者在 pom.xml 的 <project> 标签下添加:

    <repositories>
        <repository>
            <id>maven-public</id>
            <name>maven-public</name>
            <url>https://maven.aliyun.com/repository/public</url>
            <layout>default</layout>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

maven 配置腾讯云的代理仓库

类似的,配置如下:

<mirror>
    <id>nexus-tencentyun</id>
    <mirrorOf>*</mirrorOf>
    <name>Nexus tencentyun</name>
    <url>http://mirrors.cloud.tencent.com/nexus/repository/maven-public/</url>
</mirror> 

更多仓库和配置方式,可以参考:

基于maven的多模块项目

项目结构:

.
├── child1
│   ├── pom.xml
│   ├── src
│       └── main
│           └── java
│               └── demo1
│                   └── Calculate.java
├── child2
│   ├── pom.xml
│   ├── src
│       └── main
│           └── java
│               └── demo2
│                   └── CalculateService.java
├── pom.xml

项目根目录下的 pom.xml 要指定有哪些module:

<?xml version="1.0" encoding="UTF-8"?>
<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.example</groupId>
    <artifactId>java-maven-moudle-demo</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>

    <!--指定module-->
    <modules>
        <module>child1</module>
        <module>child2</module>
    </modules>

</project>

child1 和 child2 要在 parent 中指定父项目,同时child2 声明对child1 的依赖。因为 CalculateService 类要用到 Calculate 类。

child1 pom.xml 内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <groupId>com.example</groupId>
        <artifactId>java-maven-moudle-demo</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>child1</artifactId>

</project>

child2 pom.xml 内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
    <groupId>com.example</groupId>
    <artifactId>java-maven-moudle-demo</artifactId>
    <version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>child2</artifactId>

<dependencies>
    <dependency>
        <groupId>com.example</groupId>
        <artifactId>child1</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
</dependencies>

</project>

child1 模块中的 Calculate 类内容如下:

package demo1;

public class Calculate {

    public static int add(int a, int b) {
        return a+b;
    }

}

child2 模块中的 CalculateService 类内容如下:

package demo2;

import demo1.Calculate;

public class CalculateService {

    public static void main(String[] args) {
        int result = Calculate.add(1, 2);
        System.out.println(result);
    }

}

然后打开 Intellij IDEA ,File -> Open ,选中项目根目录的 pom.xml ,选择Open as project 打开即可。

如此,我们在IDEA中运行child2中的CalculateService,结果如下:

3


( 本文完 )