mirror of https://github.com/apache/maven.git
[MNG-5714] Add Merger for Maven Toolchain
This commit is contained in:
parent
0343c52601
commit
5c84bd33ba
|
@ -0,0 +1,103 @@
|
|||
package org.apache.maven.toolchain.merge;
|
||||
|
||||
/*
|
||||
* 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.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.maven.toolchain.model.PersistedToolchains;
|
||||
import org.apache.maven.toolchain.model.ToolchainModel;
|
||||
import org.codehaus.plexus.util.xml.Xpp3Dom;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Robert Scholte
|
||||
* @since 3.2.4
|
||||
*/
|
||||
public class MavenToolchainMerger
|
||||
{
|
||||
|
||||
public void merge( PersistedToolchains dominant, PersistedToolchains recessive, String recessiveSourceLevel )
|
||||
{
|
||||
if ( dominant == null || recessive == null )
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
recessive.setSourceLevel( recessiveSourceLevel );
|
||||
|
||||
shallowMerge( dominant.getToolchains(), recessive.getToolchains(), recessiveSourceLevel );
|
||||
}
|
||||
|
||||
private void shallowMerge( List<ToolchainModel> dominant, List<ToolchainModel> recessive,
|
||||
String recessiveSourceLevel )
|
||||
{
|
||||
Map<Object, ToolchainModel> merged = new LinkedHashMap<Object, ToolchainModel>();
|
||||
|
||||
for ( ToolchainModel dominantModel : dominant )
|
||||
{
|
||||
Object key = getToolchainModelKey( dominantModel );
|
||||
|
||||
merged.put( key, dominantModel );
|
||||
}
|
||||
|
||||
for ( ToolchainModel recessiveModel : recessive )
|
||||
{
|
||||
Object key = getToolchainModelKey( recessiveModel );
|
||||
|
||||
ToolchainModel dominantModel = merged.get( key );
|
||||
if ( dominantModel == null )
|
||||
{
|
||||
recessiveModel.setSourceLevel( recessiveSourceLevel );
|
||||
dominant.add( recessiveModel );
|
||||
}
|
||||
else
|
||||
{
|
||||
mergeToolchainModel_Configuration( dominantModel, recessiveModel );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected void mergeToolchainModel_Configuration( ToolchainModel target,
|
||||
ToolchainModel source )
|
||||
{
|
||||
Xpp3Dom src = (Xpp3Dom) source.getConfiguration();
|
||||
if ( src != null )
|
||||
{
|
||||
Xpp3Dom tgt = (Xpp3Dom) target.getConfiguration();
|
||||
if ( tgt == null )
|
||||
{
|
||||
tgt = Xpp3Dom.mergeXpp3Dom( new Xpp3Dom( src ), tgt );
|
||||
}
|
||||
else
|
||||
{
|
||||
tgt = Xpp3Dom.mergeXpp3Dom( tgt, src );
|
||||
}
|
||||
target.setConfiguration( tgt );
|
||||
}
|
||||
}
|
||||
|
||||
protected Object getToolchainModelKey( ToolchainModel model )
|
||||
{
|
||||
return model;
|
||||
}
|
||||
|
||||
}
|
|
@ -120,6 +120,7 @@
|
|||
<field>
|
||||
<name>type</name>
|
||||
<version>1.0.0+</version>
|
||||
<!-- <identifier>true</identifier> -->
|
||||
<description>
|
||||
<![CDATA[Type of toolchain:<ul>
|
||||
<li><code>jdk</code> for
|
||||
|
@ -150,6 +151,7 @@
|
|||
<type>String</type>
|
||||
<multiplicity>*</multiplicity>
|
||||
</association>
|
||||
<!-- <identifier>true</identifier> -->
|
||||
<description>
|
||||
<![CDATA[
|
||||
<p>Toolchain identification information, which will be matched against project requirements.</p>
|
||||
|
@ -172,6 +174,57 @@
|
|||
]]></description>
|
||||
</field>
|
||||
</fields>
|
||||
<codeSegments>
|
||||
<codeSegment>
|
||||
<version>1.1.0+</version>
|
||||
<comment>Generated hashCode() and equals() based on identifier also calls its super, which breaks comparison</comment>
|
||||
<code>
|
||||
<![CDATA[
|
||||
/**
|
||||
* Method hashCode.
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public int hashCode()
|
||||
{
|
||||
int result = 17;
|
||||
|
||||
result = 37 * result + ( type != null ? type.hashCode() : 0 );
|
||||
result = 37 * result + ( provides != null ? provides.hashCode() : 0 );
|
||||
|
||||
return result;
|
||||
} //-- int hashCode()
|
||||
|
||||
/**
|
||||
* Method equals.
|
||||
*
|
||||
* @param other
|
||||
* @return boolean
|
||||
*/
|
||||
public boolean equals( Object other )
|
||||
{
|
||||
if ( this == other )
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if ( !( other instanceof ToolchainModel ) )
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
ToolchainModel that = (ToolchainModel) other;
|
||||
boolean result = true;
|
||||
|
||||
result = result && ( getType() == null ? that.getType() == null : getType().equals( that.getType() ) );
|
||||
result = result && ( getProvides() == null ? that.getProvides() == null : getProvides().equals( that.getProvides() ) );
|
||||
|
||||
return result;
|
||||
} //-- boolean equals( Object )
|
||||
]]>
|
||||
</code>
|
||||
</codeSegment>
|
||||
</codeSegments>
|
||||
</class>
|
||||
</classes>
|
||||
</model>
|
||||
|
|
|
@ -0,0 +1,176 @@
|
|||
package org.apache.maven.toolchain.merge;
|
||||
|
||||
/*
|
||||
* 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 static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.apache.maven.toolchain.model.PersistedToolchains;
|
||||
import org.apache.maven.toolchain.model.ToolchainModel;
|
||||
import org.apache.maven.toolchain.model.TrackableBase;
|
||||
import org.apache.maven.toolchain.model.io.xpp3.MavenToolchainsXpp3Reader;
|
||||
import org.codehaus.plexus.util.IOUtil;
|
||||
import org.codehaus.plexus.util.xml.Xpp3Dom;
|
||||
import org.junit.Test;
|
||||
|
||||
public class MavenToolchainMergerTest
|
||||
{
|
||||
private MavenToolchainMerger merger = new MavenToolchainMerger();
|
||||
|
||||
private MavenToolchainsXpp3Reader reader = new MavenToolchainsXpp3Reader();
|
||||
|
||||
@Test
|
||||
public void testMergeNulls()
|
||||
{
|
||||
merger.merge( null, null, null );
|
||||
|
||||
PersistedToolchains pt = new PersistedToolchains();
|
||||
merger.merge( pt, null, null );
|
||||
merger.merge( null, pt, null );
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMergeJdk() throws Exception
|
||||
{
|
||||
InputStream isDominant = null;
|
||||
InputStream isRecessive = null;
|
||||
try
|
||||
{
|
||||
isDominant = ToolchainModel.class.getResourceAsStream( "toolchains-jdks.xml" );
|
||||
isRecessive = ToolchainModel.class.getResourceAsStream( "toolchains-jdks.xml" );
|
||||
|
||||
PersistedToolchains dominant = reader.read( isDominant );
|
||||
PersistedToolchains recessive = reader.read( isRecessive );
|
||||
assertEquals( 2, dominant.getToolchains().size() );
|
||||
|
||||
merger.merge( dominant, recessive, TrackableBase.USER_LEVEL );
|
||||
assertEquals( 2, dominant.getToolchains().size() );
|
||||
}
|
||||
finally
|
||||
{
|
||||
IOUtil.close( isDominant );
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMergeJdkExtra() throws Exception
|
||||
{
|
||||
InputStream jdksIS = null;
|
||||
InputStream jdksExtraIS = null;
|
||||
try
|
||||
{
|
||||
jdksIS = ToolchainModel.class.getResourceAsStream( "toolchains-jdks.xml" );
|
||||
jdksExtraIS = ToolchainModel.class.getResourceAsStream( "toolchains-jdks-extra.xml" );
|
||||
|
||||
PersistedToolchains jdks = reader.read( jdksIS );
|
||||
PersistedToolchains jdksExtra = reader.read( jdksExtraIS );
|
||||
assertEquals( 2, jdks.getToolchains().size() );
|
||||
|
||||
merger.merge( jdks, jdksExtra, TrackableBase.USER_LEVEL );
|
||||
assertEquals( 4, jdks.getToolchains().size() );
|
||||
assertEquals( 2, jdksExtra.getToolchains().size() );
|
||||
}
|
||||
finally
|
||||
{
|
||||
IOUtil.close( jdksIS );
|
||||
IOUtil.close( jdksExtraIS );
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
jdksIS = ToolchainModel.class.getResourceAsStream( "toolchains-jdks.xml" );
|
||||
jdksExtraIS = ToolchainModel.class.getResourceAsStream( "toolchains-jdks-extra.xml" );
|
||||
|
||||
PersistedToolchains jdks = reader.read( jdksIS );
|
||||
PersistedToolchains jdksExtra = reader.read( jdksExtraIS );
|
||||
assertEquals( 2, jdks.getToolchains().size() );
|
||||
|
||||
// switch dominant with reccessive
|
||||
merger.merge( jdksExtra, jdks, TrackableBase.USER_LEVEL );
|
||||
assertEquals( 4, jdksExtra.getToolchains().size() );
|
||||
assertEquals( 2, jdks.getToolchains().size() );
|
||||
}
|
||||
finally
|
||||
{
|
||||
IOUtil.close( jdksIS );
|
||||
IOUtil.close( jdksExtraIS );
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMergeJdkExtend() throws Exception
|
||||
{
|
||||
InputStream jdksIS = null;
|
||||
InputStream jdksExtendIS = null;
|
||||
try
|
||||
{
|
||||
jdksIS = ToolchainModel.class.getResourceAsStream( "toolchains-jdks.xml" );
|
||||
jdksExtendIS = ToolchainModel.class.getResourceAsStream( "toolchains-jdks-extend.xml" );
|
||||
|
||||
PersistedToolchains jdks = reader.read( jdksIS );
|
||||
PersistedToolchains jdksExtend = reader.read( jdksExtendIS );
|
||||
assertEquals( 2, jdks.getToolchains().size() );
|
||||
|
||||
merger.merge( jdks, jdksExtend, TrackableBase.USER_LEVEL );
|
||||
assertEquals( 2, jdks.getToolchains().size() );
|
||||
Xpp3Dom config0 = (Xpp3Dom) jdks.getToolchains().get( 0 ).getConfiguration();
|
||||
assertEquals( "lib/tools.jar", config0.getChild( "toolsJar" ).getValue() );
|
||||
assertEquals( 2, config0.getChildCount() );
|
||||
Xpp3Dom config1 = (Xpp3Dom) jdks.getToolchains().get( 1 ).getConfiguration();
|
||||
assertEquals( 2, config1.getChildCount() );
|
||||
assertEquals( "lib/classes.jar", config1.getChild( "toolsJar" ).getValue() );
|
||||
assertEquals( 2, jdksExtend.getToolchains().size() );
|
||||
}
|
||||
finally
|
||||
{
|
||||
IOUtil.close( jdksIS );
|
||||
IOUtil.close( jdksExtendIS );
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
jdksIS = ToolchainModel.class.getResourceAsStream( "toolchains-jdks.xml" );
|
||||
jdksExtendIS = ToolchainModel.class.getResourceAsStream( "toolchains-jdks-extend.xml" );
|
||||
|
||||
PersistedToolchains jdks = reader.read( jdksIS );
|
||||
PersistedToolchains jdksExtend = reader.read( jdksExtendIS );
|
||||
assertEquals( 2, jdks.getToolchains().size() );
|
||||
|
||||
// switch dominant with reccessive
|
||||
merger.merge( jdksExtend, jdks, TrackableBase.USER_LEVEL );
|
||||
assertEquals( 2, jdksExtend.getToolchains().size() );
|
||||
Xpp3Dom config0 = (Xpp3Dom) jdksExtend.getToolchains().get( 0 ).getConfiguration();
|
||||
assertEquals( "lib/tools.jar", config0.getChild( "toolsJar" ).getValue() );
|
||||
assertEquals( 2, config0.getChildCount() );
|
||||
Xpp3Dom config1 = (Xpp3Dom) jdksExtend.getToolchains().get( 1 ).getConfiguration();
|
||||
assertEquals( 2, config1.getChildCount() );
|
||||
assertEquals( "lib/classes.jar", config1.getChild( "toolsJar" ).getValue() );
|
||||
assertEquals( 2, jdks.getToolchains().size() );
|
||||
}
|
||||
finally
|
||||
{
|
||||
IOUtil.close( jdksIS );
|
||||
IOUtil.close( jdksExtendIS );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,45 @@
|
|||
<?xml version="1.0" encoding="UTF8"?>
|
||||
|
||||
<!--
|
||||
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.
|
||||
-->
|
||||
|
||||
<toolchains>
|
||||
<toolchain>
|
||||
<type>jdk</type>
|
||||
<provides>
|
||||
<version>1.5</version>
|
||||
<vendor>sun</vendor>
|
||||
</provides>
|
||||
<configuration>
|
||||
<jdkHome>${env.JAVA_HOME}</jdkHome>
|
||||
<toolsJar>lib/tools.jar</toolsJar>
|
||||
</configuration>
|
||||
</toolchain>
|
||||
<toolchain>
|
||||
<type>jdk</type>
|
||||
<provides>
|
||||
<version>1.6</version>
|
||||
<vendor>sun</vendor>
|
||||
</provides>
|
||||
<configuration>
|
||||
<jdkHome>${env.JAVA_HOME}</jdkHome>
|
||||
<toolsJar>lib/classes.jar</toolsJar>
|
||||
</configuration>
|
||||
</toolchain>
|
||||
</toolchains>
|
Loading…
Reference in New Issue