optional
adj.
可选择的;选修的。

当project-A 依赖project-B, project-B 依赖project-C时

如果在B的pom.xml中将依赖C的optional指定为true

<dependency>

<groupId>sample.ProjectC</groupId>

<artifactId>ProjectC</artifactId>

<version>1.0-SNAPSHOT</version>

<optional>true</optional>

</dependency>

project-A中如果没有显式的手动引入project-C, 则project-A不会依赖project-C, 即project-A可以自己选择是否依赖project-C

如果不在B中配置依赖C的optional树形为true,则默认值为false, 此时A会自动依赖C。

项目springdataredis的pom.xml,我们引入了spring-data-redis这个dependency.

  

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>
    <version>2.7.0</version>
</dependency>

我们看到jedis和lettuce均有optional为true的标示,所以我们需要自己引入选择使用哪个redis的驱动。

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">

	<modelVersion>4.0.0</modelVersion>

	<groupId>org.springframework.data</groupId>
	<artifactId>spring-data-redis</artifactId>
	<version>2.7.0</version>

	<name>Spring Data Redis</name>
	<description>Spring Data module for Redis</description>
	<url>https://spring.io/projects/spring-data-redis</url>

	<parent>
		<groupId>org.springframework.data.build</groupId>
		<artifactId>spring-data-parent</artifactId>
		<version>2.7.0</version>
	</parent>

	<properties>
		<springdata.keyvalue>2.7.0</springdata.keyvalue>
		<awaitility>4.0.2</awaitility>
		<jta>1.1</jta>
		<beanutils>1.9.4</beanutils>
		<xstream>1.4.19</xstream>
		<pool>2.11.1</pool>
		<lettuce>6.1.8.RELEASE</lettuce>
		<jedis>3.8.0</jedis>
		<multithreadedtc>1.01</multithreadedtc>
		<netty>4.1.72.Final</netty>
		<java-module-name>spring.data.redis</java-module-name>
	</properties>

	<scm>
		<connection>scm:git:https://github.com/spring-projects/spring-data-redis.git</connection>
		<developerConnection>scm:git:git@github.com:spring-projects/spring-data-redis.git</developerConnection>
		<url>https://github.com/spring-projects/spring-data-redis</url>
	</scm>

	<issueManagement>
		<system>GitHub</system>
		<url>https://github.com/spring-projects/spring-data-redis/issues</url>
	</issueManagement>

	<dependencyManagement>
		<dependencies>

			<dependency>
				<groupId>org.apache.commons</groupId>
				<artifactId>commons-pool2</artifactId>
				<version>${pool}</version>
			</dependency>

		</dependencies>
	</dependencyManagement>

	<dependencies>

		<!-- Spring -->

		<dependency>
			<groupId>org.springframework.data</groupId>
			<artifactId>spring-data-keyvalue</artifactId>
			<version>${springdata.keyvalue}</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-oxm</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
		</dependency>

		<!-- REDIS Drivers -->

		<dependency>
			<groupId>redis.clients</groupId>
			<artifactId>jedis</artifactId>
			<version>${jedis}</version>
			<optional>true</optional>
		</dependency>

		<dependency>
			<groupId>io.lettuce</groupId>
			<artifactId>lettuce-core</artifactId>
			<version>${lettuce}</version>
			<optional>true</optional>
		</dependency>

		<dependency>
			<groupId>io.netty</groupId>
			<artifactId>netty-transport-native-epoll</artifactId>
			<classifier>linux-x86_64</classifier>
			<version>${netty}</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>io.netty</groupId>
			<artifactId>netty-transport-native-kqueue</artifactId>
			<classifier>osx-x86_64</classifier>
			<version>${netty}</version>
			<scope>test</scope>
		</dependency>

		<!-- reactive -->
		<dependency>
			<groupId>io.projectreactor</groupId>
			<artifactId>reactor-core</artifactId>
			<optional>true</optional>
		</dependency>

		<!--Mapper -->

		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-core</artifactId>
			<optional>true</optional>
		</dependency>

		<dependency>
			<groupId>com.fasterxml.jackson.datatype</groupId>
			<artifactId>jackson-datatype-jsr310</artifactId>
			<optional>true</optional>
		</dependency>

		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<optional>true</optional>
		</dependency>

		<dependency>
			<groupId>commons-beanutils</groupId>
			<artifactId>commons-beanutils</artifactId>
			<version>${beanutils}</version>
			<optional>true</optional>
		</dependency>

		<!-- Pool -->

		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-pool2</artifactId>
			<optional>true</optional>
		</dependency>

		<!-- CDI -->
		<!-- Dependency order required to build against CDI 1.0 and test with CDI 2.0 -->
		<dependency>
			<groupId>org.apache.geronimo.specs</groupId>
			<artifactId>geronimo-jcdi_2.0_spec</artifactId>
			<version>1.0.1</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>javax.interceptor</groupId>
			<artifactId>javax.interceptor-api</artifactId>
			<version>1.2.1</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>javax.enterprise</groupId>
			<artifactId>cdi-api</artifactId>
			<version>${cdi}</version>
			<scope>provided</scope>
			<optional>true</optional>
		</dependency>

		<dependency>
			<groupId>javax.annotation</groupId>
			<artifactId>javax.annotation-api</artifactId>
			<version>${javax-annotation-api}</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>org.apache.openwebbeans</groupId>
			<artifactId>openwebbeans-se</artifactId>
			<version>${webbeans}</version>
			<scope>test</scope>
		</dependency>

		<!-- Kotlin extension -->
		<dependency>
			<groupId>org.jetbrains.kotlin</groupId>
			<artifactId>kotlin-stdlib</artifactId>
			<optional>true</optional>
		</dependency>

		<dependency>
			<groupId>org.jetbrains.kotlin</groupId>
			<artifactId>kotlin-reflect</artifactId>
			<optional>true</optional>
		</dependency>

		<dependency>
			<groupId>org.jetbrains.kotlinx</groupId>
			<artifactId>kotlinx-coroutines-core</artifactId>
			<version>${kotlin-coroutines}</version>
			<optional>true</optional>
		</dependency>

		<dependency>
			<groupId>org.jetbrains.kotlinx</groupId>
			<artifactId>kotlinx-coroutines-reactor</artifactId>
			<version>${kotlin-coroutines}</version>
			<optional>true</optional>
		</dependency>

		<dependency>
			<groupId>io.mockk</groupId>
			<artifactId>mockk</artifactId>
			<version>${mockk}</version>
			<scope>test</scope>
		</dependency>

		<!-- Test -->

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>org.awaitility</groupId>
			<artifactId>awaitility</artifactId>
			<version>${awaitility}</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>javax.transaction</groupId>
			<artifactId>jta</artifactId>
			<version>${jta}</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>javax.annotation</groupId>
			<artifactId>jsr250-api</artifactId>
			<version>1.0</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>com.thoughtworks.xstream</groupId>
			<artifactId>xstream</artifactId>
			<version>${xstream}</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>edu.umd.cs.mtc</groupId>
			<artifactId>multithreadedtc</artifactId>
			<version>${multithreadedtc}</version>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>io.projectreactor</groupId>
			<artifactId>reactor-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>

			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
				<configuration>
					<!-- Retain stack traces -->
					<argLine>-XX:-OmitStackTraceInFastThrow</argLine>
					<useSystemClassLoader>false</useSystemClassLoader>
					<includes>
						<include>**/*Tests.java</include>
						<include>**/*Test.java</include>
					</includes>
				</configuration>
			</plugin>

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

			<plugin>
				<groupId>org.asciidoctor</groupId>
				<artifactId>asciidoctor-maven-plugin</artifactId>
				<configuration>
					<attributes>
						<lettuce>${lettuce}</lettuce>
						<jedis>${jedis}</jedis>
					</attributes>
				</configuration>
			</plugin>

		</plugins>
	</build>

	<repositories>
		<repository>
			<id>spring-libs-release</id>
			<url>https://repo.spring.io/libs-release</url>
		</repository>
	</repositories>

	<pluginRepositories>
		<pluginRepository>
			<id>spring-plugins-release</id>
			<url>https://repo.spring.io/plugins-release</url>
		</pluginRepository>
	</pluginRepositories>

	<profiles>
		<profile>
			<!-- placeholder for no profile -->
			<id>none</id>
		</profile>
	</profiles>
</project>

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐