一个简单的springboot微服务项目,所有子模块依赖api-root这个POM
公共的jar中因为没有main方法,当我们install整个项目的时候,会抛异常:
解决办法:在公共jar包的POM中加入
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
而服务jar我们通常不需要deploy到私服仓库中去,只需要在服务模块的POM中加入
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
deploy我们需要告诉它发布到哪去,只需要在父POM中加入下面的代码:
<distributionManagement>
<!-- 两个ID必须与 setting.xml中的<server><id>nexus-releases</id></server>保持一致-->
<repository>
<id>nexus-releases</id>
<name>Nexus Release Repository</name>
<url>http://192.168.1.2:8081/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>nexus-snapshots</id>
<name>Nexus Snapshot Repository</name>
<url>http://192.168.1.2:8081/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
上面的<id>和maven的settings.xml中配置的id要对应
<servers>
<server>
<id>nexus-releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>nexus-snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
</servers>
这样当我们运行mvn deploy -Dmanve.test.skip=true
的时候,就可以将api-root.pom,commons.jar,api-feign.jar发布到私服仓库中供别人使用了。
有时候我们自己的公共的snapshots包deploy到私服后,maven并没有去拉取最新的jar包,这是因为maven默认配置每天只拉取一次。
方案一:
可以在setting.xml文件中修改默认行为:
<updatePolicy>always</updatePolicy>
updatePolicy
可用的值包括:
-
daily
每天检查一次(默认) -
never
从不检查更新; -
always
每次构建都检查更新; -
interval:X
每隔X分钟检查一次更新(X为任意整数)
方案二:
或者打包的时候添加-U
参数来强制更新。
mvn clean package -U
方案三:
idea中还可以勾选Always update snapshots
附:
- maven的setting.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">
<pluginGroups>
</pluginGroups>
<proxies>
</proxies>
<servers>
<server>
<id>nexus-releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>nexus-snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
</servers>
<mirrors>
<mirror>
<id>maven-public</id>
<name>maven-public</name>
<url>http://192.168.1.2:8081/repository/maven-public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
<profiles>
<profile>
<id>maven-public</id>
<repositories>
<repository>
<id>nexus-releases</id> <!--正式仓库id-->
<!--name随便-->
<name>Nexus Release Snapshot Repository</name>
<!--地址是nexus中repository(Releases/Snapshots)中对应的地址-->
<url>http://192.168.1.2:8081/repository/maven-releases/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>nexus-snapshots</id>
<url>http://192.168.1.2:8081/repository/maven-snapshots/</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
<updatePolicy>always</updatePolicy>
</repository>
</repositories>
<pluginRepositories> <!--插件仓库地址,各节点的含义和上面是一样的-->
<pluginRepository>
<id>nexus-releases</id>
<name>Nexus Release Snapshot Repository</name>
<url>http://192.168.1.2:8081/repository/maven-releases/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>nexus-snapshots</id>
<url>http://192.168.1.2:8081/repository/maven-snapshots/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
<updatePolicy>always</updatePolicy>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<!--激活配置-->
<activeProfiles>
<activeProfile>maven-public</activeProfile> <!--profile下的id-->
</activeProfiles>
</settings>
- 完整的api-root的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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wd.cloud</groupId>
<artifactId>api-root</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>api-root</name>
<description>微服务根POM</description>
<modules>
<module>api-feign</module><!--内部互调接口定义-->
<module>api-gateway</module><!--api网关-->
<module>config-server</module><!--配置中心-->
<module>discovery</module><!--服务注册中心-->
<module>auth-server</module><!--鉴权服务-->
<module>doc-delivery</module><!--文献传递服务-->
<module>commons</module><!--提取的公共方法和类-->
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.M9</spring-cloud.version>
<openfeign.version>1.4.4.RELEASE</openfeign.version>
<com.alibaba.druid.version>1.1.9</com.alibaba.druid.version>
<javax.mail.version>1.4.7</javax.mail.version>
<swagger.version>1.7.0.RELEASE</swagger.version>
<cn.hutool.all.version>[4.0.12,)</cn.hutool.all.version>
<jackson.databind.version>2.9.5</jackson.databind.version>
<jackson.core.version>2.9.5</jackson.core.version>
<jackson.asl.version>1.9.12</jackson.asl.version>
</properties>
<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>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${com.alibaba.druid.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-feign</artifactId>
<version>${openfeign.version}</version>
</dependency>
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>swagger-spring-boot-starter</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>${javax.mail.version}</version>
</dependency>
<dependency>
<groupId>com.wd.cloud</groupId>
<artifactId>commons</artifactId>
<version>${version}</version>
</dependency>
<dependency>
<groupId>com.wd.cloud</groupId>
<artifactId>api-feign</artifactId>
<version>${version}</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>${cn.hutool.all.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.databind.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.core.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>${jackson.asl.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<distributionManagement>
<!-- 两个ID必须与 setting.xml中的<server><id>nexus-releases</id></server>保持一致-->
<repository>
<id>nexus-releases</id>
<name>Nexus Release Repository</name>
<url>http://192.168.1.2:8081/repository/maven-releases/</url>
</repository>
<snapshotRepository>
<id>nexus-snapshots</id>
<name>Nexus Snapshot Repository</name>
<url>http://192.168.1.2:8081/repository/maven-snapshots/</url>
</snapshotRepository>
</distributionManagement>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/libs-milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
- 公共模块commons的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">
<modelVersion>4.0.0</modelVersion>
<version>${parent.version}</version>
<artifactId>commons</artifactId>
<packaging>jar</packaging>
<name>commons</name>
<description>公共包</description>
<parent>
<groupId>com.wd.cloud</groupId>
<artifactId>api-root</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</project>
- 服务模块config-server的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">
<modelVersion>4.0.0</modelVersion>
<artifactId>config-server</artifactId>
<version>${parent.version}</version>
<packaging>jar</packaging>
<name>config-server</name>
<description>配置服务中心</description>
<parent>
<groupId>com.wd.cloud</groupId>
<artifactId>api-root</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.wd.cloud</groupId>
<artifactId>commons</artifactId>
</dependency>
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>swagger-spring-boot-starter</artifactId>
</dependency>
</dependencies>
<build>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
</project>