在eclipse平台下利用maven开发多模块项目

本文介绍如何使用Maven结合Eclipse进行项目管理,包括Maven的安装配置、私服设置及多模块项目的搭建过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用maven+eclipse 结合的方式开发项目  (其中eclipse可以使用eclipse-maven插件或者在eclipse中的window->preferences->maven->Installations配置系统已经安装的maven)

主要用于项目管理,当项目拆成多模块时,每个模块看成独立部分,可以有着自己的独立发布版本,供项目其他模块直接引用jar包使用。这样的话,可以使项目组中的其他成员专心开发自己的模块。

我没有使用eclipse的maven插件,而是配置了系统安装的maven

首先下载maven

自己下载的是apache-maven-3.3.9

配置环境变量MAVEN_HOME;在安装目录下conf中有一个settings.xml的配置文件。在用户目录下C:\Users\a\.m2下也有个settings.xml配置文件。

对于maven的配置$MAVEN_HOME/settings.xml为全局配置,~/.m2/settings.xml为用户级配置,项目下pom.xml为项目级配置。

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <interactiveMode>true</interactiveMode>
    <offline>false</offline>
    <pluginGroups>
        <pluginGroup>org.mortbay.jetty</pluginGroup>
        <pluginGroup>org.jenkins-ci.tools</pluginGroup>
    </pluginGroups>
    <!--配置权限,使用默认用户-->
    <servers>
        <server>
            <id>nexus-releases</id>  <!--  与项目中的pom.xml中的distributionManagement 的id一致-->
            <username>admin</username>
            <password>admin123</password>
        </server>
        <server>
            <id>nexus-snapshots</id>
            <username>admin</username>
            <password>admin123</password>
        </server>
    </servers>    
 <mirrors>
    </mirrors>
    <profiles>
        <profile>  <!-- 可以用于需要根据不同的环境采用不同的maven策略例如分为开发,测试,正式环境-->
            <id>first</id>
            <activation>
                <activeByDefault>false</activeByDefault>
                <jdk>1.6</jdk>
            </activation>
            <repositories>
                <!-- 私有库地址-->
                <repository>
                    <id>nexus</id>

                    <!--  http://127.0.0.1:8081/nexus/content/groups/public为本机安装的nexus私服 -->
                    <url>http://127.0.0.1:8081/nexus/content/groups/public/</url> 
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </repository>
            </repositories>
           <properties>  
            <sonar.host.url>http://127.0.0.1:9000</sonar.host.url>   <!--  http://127.0.0.1:9000为本机安装的sonar(代码质量检查)服务器-->

           <!--  maven检查代码质量命令使用sonar:sonar- -->

             </properties>  
            <pluginRepositories>
                <!--插件库地址-->
                <pluginRepository>
                    <id>nexus</id>

                    <!--  http://127.0.0.1:8081/nexus/content/groups/public为本机安装的nexus私服 -->
                    <url>http://127.0.0.1:8081/nexus/content/groups/public/</url> 
                    <releases>  
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </pluginRepository>
            </pluginRepositories>
        </profile>
    </profiles>
    <!--激活profile-->
    <activeProfiles>
        <activeProfile>first</activeProfile>
    </activeProfiles>
</settings>

2.然后使用eclipse建立maven项目

example-root

<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>example-root</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>example-root</name>
<url>http://maven.apache.org</url>

<!-- 发布管理的地址 其中repository中的id(nexus-releases与nexus-snapshots需要用户名和密码在用户目录下的setting,xml中有配置)-->
<distributionManagement>
<repository>
<id>nexus-releases</id>
<name>Nexus Release Repository</name>
<url>http://127.0.0.1:8081/nexus/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>nexus-snapshots</id>
<name>Nexus Snapshot Repository</name>
<url>http://192.168.103.209:8081/nexus/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<example-config.version>1.0-SNAPSHOT</example-config.version>
<example-common.version>1.0-SNAPSHOT</example-common.version>
<jdk.version>1.7</jdk.version>
<spring.version>3.2.4.RELEASE</spring.version>
<org.slf4j-version>1.7.21</org.slf4j-version>
</properties>

      <!-- 所有子项目均必须的依赖 -->
<dependencies>  <!--  由于继承,所有引用root的pom.xml都会依赖-->
<!-- Test Dependency Begin -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<!-- Test Dependency End -->
</dependencies>


<!-- 子项目可选依赖 -->

<!--  依赖管理,这里不是依赖,只是当项目中其他模块引用root模块时并且确定dependency一些jar包不需要明确版本号,均以此为准,防止项目中jar包引用冲突-->
<dependencyManagement>
<dependencies>
<!-- servlet -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<!-- common -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!-- Spring Session做分布式会话管理 -->
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session</artifactId>
<version>1.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.5.2.RELEASE</version>
</dependency>
<!-- Redis client -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
<version>1.2.2.RELEASE</version>
</dependency>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
<configuration>
<uniqueVersion>false</uniqueVersion>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.10</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<failOnError>true</failOnError>
<verbose>true</verbose>
<fork>true</fork>
<compilerArgument>-nowarn</compilerArgument>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.0.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<defaultGoal>compile</defaultGoal>
</build>
</project>

3.example-core maven项目

<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>
<parent>
<groupId>com.example</groupId>
<artifactId>example-core</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>com.example</groupId>
<artifactId>example-core</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>example-core</name>
<packaging>jar</packaging>

<!-- 下载使用的资源库私服 此处不要是因为在maven用户目录下的setting.xml已经配置了-->
<!-- <repositories>
<repository>
<id>nexus</id>
<url>http://192.168.103.209:8081/nexus/content/groups/public/</url>
</repository>
</repositories> -->
</project>


使用eclipse右键项目 Run As->maven install/Run As->Run configurations->maven build->goals deploy发布,将该项目打包发布到配置的私服nexus上

参考网址:http://www.cnblogs.com/quanyongan/archive/2013/05/28/3103243.html


其实对于maven goals这块不懂,现在记录在次,在以后的学习中再进一步学习记录。

参考网址:

http://blog.youkuaiyun.com/bluishglc/article/details/6632280#comments

http://juvenshun.iteye.com/blog/213959

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值