1 package org.apache.maven.plugins.enforcer;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.Arrays;
23 import java.util.Iterator;
24 import java.util.List;
25
26 import org.apache.commons.lang3.SystemUtils;
27 import org.apache.maven.artifact.versioning.ArtifactVersion;
28 import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
29 import org.apache.maven.enforcer.rule.api.EnforcerRuleException;
30 import org.apache.maven.enforcer.rule.api.EnforcerRuleHelper;
31 import org.apache.maven.plugin.logging.Log;
32 import org.codehaus.plexus.util.StringUtils;
33
34
35
36
37
38
39
40 public class RequireJavaVersion
41 extends AbstractVersionEnforcer
42 {
43 @Override
44 public void execute( EnforcerRuleHelper helper )
45 throws EnforcerRuleException
46 {
47 String javaVersion = SystemUtils.JAVA_VERSION;
48 Log log = helper.getLog();
49
50 log.debug( "Detected Java String: '" + javaVersion + "'" );
51 javaVersion = normalizeJDKVersion( javaVersion );
52 log.debug( "Normalized Java String: '" + javaVersion + "'" );
53
54 ArtifactVersion detectedJdkVersion = new DefaultArtifactVersion( javaVersion );
55
56 log.debug( "Parsed Version: Major: " + detectedJdkVersion.getMajorVersion() + " Minor: "
57 + detectedJdkVersion.getMinorVersion() + " Incremental: " + detectedJdkVersion.getIncrementalVersion()
58 + " Build: " + detectedJdkVersion.getBuildNumber() + " Qualifier: " + detectedJdkVersion.getQualifier() );
59
60 enforceVersion( helper.getLog(), "JDK", getVersion(), detectedJdkVersion );
61 }
62
63
64
65
66
67
68
69 public static String normalizeJDKVersion( String theJdkVersion )
70 {
71
72 theJdkVersion = theJdkVersion.replaceAll( "_|-", "." );
73 String tokenArray[] = StringUtils.split( theJdkVersion, "." );
74 List<String> tokens = Arrays.asList( tokenArray );
75 StringBuffer buffer = new StringBuffer( theJdkVersion.length() );
76
77 Iterator<String> iter = tokens.iterator();
78 for ( int i = 0; i < tokens.size() && i < 4; i++ )
79 {
80 String section = (String) iter.next();
81 section = section.replaceAll( "[^0-9]", "" );
82
83 if ( StringUtils.isNotEmpty( section ) )
84 {
85 buffer.append( Integer.parseInt( section ) );
86
87 if ( i != 2 )
88 {
89 buffer.append( '.' );
90 }
91 else
92 {
93 buffer.append( '-' );
94 }
95 }
96 }
97
98 String version = buffer.toString();
99 version = StringUtils.stripEnd( version, "-" );
100 return StringUtils.stripEnd( version, "." );
101 }
102 }