[MNG-5227] The 'optional' flag of a dependency should be manageable
diff --git a/core-it-suite/src/test/java/org/apache/maven/it/IntegrationTestSuite.java b/core-it-suite/src/test/java/org/apache/maven/it/IntegrationTestSuite.java
index e92cf86..54bf236 100644
--- a/core-it-suite/src/test/java/org/apache/maven/it/IntegrationTestSuite.java
+++ b/core-it-suite/src/test/java/org/apache/maven/it/IntegrationTestSuite.java
@@ -130,6 +130,7 @@
suite.addTestSuite( MavenITmng5753CustomMojoExecutionConfiguratorTest.class );
suite.addTestSuite( MavenITmng5716ToolchainsTypeTest.class );
suite.addTestSuite( MavenITmng5663NestedImportScopePomResolutionTest.class );
+ suite.addTestSuite( MavenITmng5227DependencyOptionalFlagManagementTest.class );
suite.addTestSuite( MavenITmng2562Timestamp322Test.class );
suite.addTestSuite( MavenITmng2199ParentVersionRangeTest.class );
suite.addTestSuite( MavenITmng5640LifecycleParticipantAfterSessionEnd.class );
diff --git a/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng5227DependencyOptionalFlagManagementTest.java b/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng5227DependencyOptionalFlagManagementTest.java
new file mode 100644
index 0000000..05464c7
--- /dev/null
+++ b/core-it-suite/src/test/java/org/apache/maven/it/MavenITmng5227DependencyOptionalFlagManagementTest.java
@@ -0,0 +1,89 @@
+package org.apache.maven.it;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.io.File;
+import java.util.List;
+import java.util.Properties;
+import org.apache.maven.it.util.ResourceExtractor;
+import static junit.framework.Assert.assertEquals;
+import static junit.framework.Assert.assertTrue;
+
+/**
+ * This is a test set for <a href="https://issues.apache.org/jira/browse/MNG-5227">MNG-5227</a>.
+ *
+ */
+public class MavenITmng5227DependencyOptionalFlagManagementTest
+ extends AbstractMavenIntegrationTestCase
+{
+
+ public MavenITmng5227DependencyOptionalFlagManagementTest()
+ {
+ super( "[3.5.1,)" );
+ }
+
+ /**
+ * Verify that a dependency's optional flag is subject to dependency management. This part of the test checks
+ * the effective model.
+ */
+ public void testitModel()
+ throws Exception
+ {
+ File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-5227/model" );
+
+ Verifier verifier = newVerifier( testDir.getAbsolutePath() );
+ verifier.setAutoclean( false );
+ verifier.deleteDirectory( "target" );
+ verifier.executeGoal( "validate" );
+ verifier.verifyErrorFreeLog();
+ verifier.resetStreams();
+
+ Properties props = verifier.loadProperties( "target/pom.properties" );
+ assertEquals( "dep", props.getProperty( "project.dependencies.0.artifactId" ) );
+ assertEquals( "true", props.getProperty( "project.dependencies.0.optional" ) );
+ }
+
+ /**
+ * Verify that a transitive dependency's optional flag is subject to dependency management of the root artifact.
+ * This part of the test checks the artifact collector.
+ */
+ public void testitResolution()
+ throws Exception
+ {
+ File testDir = ResourceExtractor.simpleExtractResources( getClass(), "/mng-5227/resolution" );
+
+ Verifier verifier = newVerifier( testDir.getAbsolutePath() );
+ verifier.setAutoclean( false );
+ verifier.deleteDirectory( "target" );
+ verifier.deleteArtifacts( "org.apache.maven.its.mng5227" );
+ verifier.filterFile( "settings-template.xml", "settings.xml", "UTF-8", verifier.newDefaultFilterProperties() );
+ verifier.addCliOption( "--settings" );
+ verifier.addCliOption( "settings.xml" );
+ verifier.executeGoal( "validate" );
+ verifier.verifyErrorFreeLog();
+ verifier.resetStreams();
+
+ List<String> classpath = verifier.loadLines( "target/classpath.txt", "UTF-8" );
+ assertTrue( classpath.toString(), classpath.contains( "direct-0.2.jar" ) );
+ assertFalse( classpath.toString(), classpath.contains( "transitive-0.1.jar" ) );
+ }
+
+}
+
diff --git a/core-it-suite/src/test/resources/mng-5227/model/pom.xml b/core-it-suite/src/test/resources/mng-5227/model/pom.xml
new file mode 100644
index 0000000..0373e9a
--- /dev/null
+++ b/core-it-suite/src/test/resources/mng-5227/model/pom.xml
@@ -0,0 +1,77 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements. See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership. The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied. See the License for the
+specific language governing permissions and limitations
+under the License.
+-->
+
+<project>
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>org.apache.maven.its.mng5227</groupId>
+ <artifactId>test</artifactId>
+ <version>0.1</version>
+ <packaging>pom</packaging>
+
+ <name>Maven Integration Test :: MNG-5227</name>
+ <description>
+ Verify that the optional flag of a dependency is controlled by dependency management.
+ </description>
+
+ <dependencyManagement>
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.maven.its.mng5227</groupId>
+ <artifactId>dep</artifactId>
+ <version>0.1</version>
+ <!-- This must not be ignored -->
+ <optional>true</optional>
+ </dependency>
+ </dependencies>
+ </dependencyManagement>
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.maven.its.mng5227</groupId>
+ <artifactId>dep</artifactId>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.its.plugins</groupId>
+ <artifactId>maven-it-plugin-expression</artifactId>
+ <version>2.1-SNAPSHOT</version>
+ <executions>
+ <execution>
+ <id>test</id>
+ <phase>validate</phase>
+ <configuration>
+ <expressions>
+ <expression>project/dependencies</expression>
+ </expressions>
+ <outputFile>target/pom.properties</outputFile>
+ </configuration>
+ <goals>
+ <goal>eval</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+</project>
diff --git a/core-it-suite/src/test/resources/mng-5227/resolution/pom.xml b/core-it-suite/src/test/resources/mng-5227/resolution/pom.xml
new file mode 100644
index 0000000..a334b20
--- /dev/null
+++ b/core-it-suite/src/test/resources/mng-5227/resolution/pom.xml
@@ -0,0 +1,76 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements. See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership. The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied. See the License for the
+specific language governing permissions and limitations
+under the License.
+-->
+
+<project>
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>org.apache.maven.its.mng5227</groupId>
+ <artifactId>test</artifactId>
+ <version>0.1</version>
+
+ <name>Maven Integration Test :: MNG-5227</name>
+ <description>
+ Verify that the optional state of a transitive dependency is controlled by dependency management.
+ </description>
+
+ <dependencyManagement>
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.maven.its.mng5227</groupId>
+ <artifactId>transitive</artifactId>
+ <version>0.1</version>
+ <!-- This must apply to transitive dependencies -->
+ <optional>true</optional>
+ </dependency>
+ </dependencies>
+ </dependencyManagement>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.maven.its.mng5227</groupId>
+ <artifactId>direct</artifactId>
+ <version>0.2</version>
+ </dependency>
+ </dependencies>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.its.plugins</groupId>
+ <artifactId>maven-it-plugin-dependency-resolution</artifactId>
+ <version>2.1-SNAPSHOT</version>
+ <configuration>
+ <compileClassPath>target/classpath.txt</compileClassPath>
+ <significantPathLevels>1</significantPathLevels>
+ </configuration>
+ <executions>
+ <execution>
+ <id>test</id>
+ <phase>validate</phase>
+ <goals>
+ <goal>compile</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ </plugins>
+ </build>
+</project>
diff --git a/core-it-suite/src/test/resources/mng-5227/resolution/repo/org/apache/maven/its/mng5227/direct/0.2/direct-0.2.jar b/core-it-suite/src/test/resources/mng-5227/resolution/repo/org/apache/maven/its/mng5227/direct/0.2/direct-0.2.jar
new file mode 100644
index 0000000..d756c3e
--- /dev/null
+++ b/core-it-suite/src/test/resources/mng-5227/resolution/repo/org/apache/maven/its/mng5227/direct/0.2/direct-0.2.jar
Binary files differ
diff --git a/core-it-suite/src/test/resources/mng-5227/resolution/repo/org/apache/maven/its/mng5227/direct/0.2/direct-0.2.pom b/core-it-suite/src/test/resources/mng-5227/resolution/repo/org/apache/maven/its/mng5227/direct/0.2/direct-0.2.pom
new file mode 100644
index 0000000..c54feb3
--- /dev/null
+++ b/core-it-suite/src/test/resources/mng-5227/resolution/repo/org/apache/maven/its/mng5227/direct/0.2/direct-0.2.pom
@@ -0,0 +1,46 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements. See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership. The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied. See the License for the
+specific language governing permissions and limitations
+under the License.
+-->
+
+<project>
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>org.apache.maven.its.mng5227</groupId>
+ <artifactId>direct</artifactId>
+ <version>0.2</version>
+ <packaging>jar</packaging>
+
+ <name>Maven Integration Test :: MNG-5227 :: Direct Dependency</name>
+
+ <distributionManagement>
+ <repository>
+ <id>maven-core-it</id>
+ <url>file:///${basedir}/repo</url>
+ </repository>
+ </distributionManagement>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.maven.its.mng5227</groupId>
+ <artifactId>transitive</artifactId>
+ <version>0.1</version>
+ </dependency>
+ </dependencies>
+</project>
diff --git a/core-it-suite/src/test/resources/mng-5227/resolution/repo/org/apache/maven/its/mng5227/direct/maven-metadata.xml b/core-it-suite/src/test/resources/mng-5227/resolution/repo/org/apache/maven/its/mng5227/direct/maven-metadata.xml
new file mode 100644
index 0000000..d0a7451
--- /dev/null
+++ b/core-it-suite/src/test/resources/mng-5227/resolution/repo/org/apache/maven/its/mng5227/direct/maven-metadata.xml
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<metadata>
+ <groupId>org.apache.maven.its.mng5227</groupId>
+ <artifactId>direct</artifactId>
+ <version>0.2</version>
+ <versioning>
+ <versions>
+ <version>0.2</version>
+ </versions>
+ <lastUpdated>20100319225359</lastUpdated>
+ </versioning>
+</metadata>
diff --git a/core-it-suite/src/test/resources/mng-5227/resolution/repo/org/apache/maven/its/mng5227/transitive/0.1/transitive-0.1.jar b/core-it-suite/src/test/resources/mng-5227/resolution/repo/org/apache/maven/its/mng5227/transitive/0.1/transitive-0.1.jar
new file mode 100644
index 0000000..2099b1c
--- /dev/null
+++ b/core-it-suite/src/test/resources/mng-5227/resolution/repo/org/apache/maven/its/mng5227/transitive/0.1/transitive-0.1.jar
Binary files differ
diff --git a/core-it-suite/src/test/resources/mng-5227/resolution/repo/org/apache/maven/its/mng5227/transitive/0.1/transitive-0.1.pom b/core-it-suite/src/test/resources/mng-5227/resolution/repo/org/apache/maven/its/mng5227/transitive/0.1/transitive-0.1.pom
new file mode 100644
index 0000000..1154080
--- /dev/null
+++ b/core-it-suite/src/test/resources/mng-5227/resolution/repo/org/apache/maven/its/mng5227/transitive/0.1/transitive-0.1.pom
@@ -0,0 +1,38 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements. See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership. The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied. See the License for the
+specific language governing permissions and limitations
+under the License.
+-->
+
+<project>
+ <modelVersion>4.0.0</modelVersion>
+
+ <groupId>org.apache.maven.its.mng5227</groupId>
+ <artifactId>transitive</artifactId>
+ <version>0.1</version>
+ <packaging>jar</packaging>
+
+ <name>Maven Integration Test :: MNG-5227 :: Transitive Dependency</name>
+
+ <distributionManagement>
+ <repository>
+ <id>maven-core-it</id>
+ <url>file:///${basedir}/repo</url>
+ </repository>
+ </distributionManagement>
+</project>
diff --git a/core-it-suite/src/test/resources/mng-5227/resolution/repo/org/apache/maven/its/mng5227/transitive/maven-metadata.xml b/core-it-suite/src/test/resources/mng-5227/resolution/repo/org/apache/maven/its/mng5227/transitive/maven-metadata.xml
new file mode 100644
index 0000000..2c77226
--- /dev/null
+++ b/core-it-suite/src/test/resources/mng-5227/resolution/repo/org/apache/maven/its/mng5227/transitive/maven-metadata.xml
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<metadata>
+ <groupId>org.apache.maven.its.mng5227</groupId>
+ <artifactId>transitive</artifactId>
+ <version>0.1</version>
+ <versioning>
+ <versions>
+ <version>0.1</version>
+ </versions>
+ <lastUpdated>20100319225254</lastUpdated>
+ </versioning>
+</metadata>
diff --git a/core-it-suite/src/test/resources/mng-5227/resolution/settings-template.xml b/core-it-suite/src/test/resources/mng-5227/resolution/settings-template.xml
new file mode 100644
index 0000000..c985d1a
--- /dev/null
+++ b/core-it-suite/src/test/resources/mng-5227/resolution/settings-template.xml
@@ -0,0 +1,43 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!--
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements. See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership. The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing,
+software distributed under the License is distributed on an
+"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+KIND, either express or implied. See the License for the
+specific language governing permissions and limitations
+under the License.
+-->
+
+<settings>
+ <profiles>
+ <profile>
+ <id>maven-core-it-repo</id>
+ <repositories>
+ <repository>
+ <id>maven-core-it</id>
+ <url>@baseurl@/repo</url>
+ <releases>
+ <checksumPolicy>ignore</checksumPolicy>
+ </releases>
+ <snapshots>
+ <enabled>false</enabled>
+ </snapshots>
+ </repository>
+ </repositories>
+ </profile>
+ </profiles>
+ <activeProfiles>
+ <activeProfile>maven-core-it-repo</activeProfile>
+ </activeProfiles>
+</settings>