1 | |
package org.apache.maven.profiles.activation; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import org.apache.maven.artifact.versioning.DefaultArtifactVersion; |
23 | |
import org.apache.maven.artifact.versioning.InvalidVersionSpecificationException; |
24 | |
import org.apache.maven.artifact.versioning.VersionRange; |
25 | |
import org.apache.maven.model.Activation; |
26 | |
import org.apache.maven.model.Profile; |
27 | |
import org.codehaus.plexus.util.StringUtils; |
28 | |
|
29 | 129 | public class JdkPrefixProfileActivator |
30 | |
extends DetectedProfileActivator |
31 | |
{ |
32 | 1 | private static final String JDK_VERSION = System.getProperty( "java.version" ); |
33 | |
|
34 | |
public boolean isActive( Profile profile ) |
35 | |
throws ProfileActivationException |
36 | |
{ |
37 | 8 | Activation activation = profile.getActivation(); |
38 | |
|
39 | 8 | String jdk = activation.getJdk(); |
40 | |
|
41 | |
|
42 | 8 | if ( jdk.startsWith( "[" ) || jdk.startsWith( "(" ) ) |
43 | |
{ |
44 | |
try |
45 | |
{ |
46 | 3 | if ( matchJdkVersionRange( jdk ) ) |
47 | |
{ |
48 | 1 | return true; |
49 | |
} |
50 | |
else |
51 | |
{ |
52 | 1 | return false; |
53 | |
} |
54 | |
} |
55 | 1 | catch ( InvalidVersionSpecificationException e ) |
56 | |
{ |
57 | 1 | throw new ProfileActivationException( "Invalid JDK version in profile '" + profile.getId() + "': " |
58 | |
+ e.getMessage() ); |
59 | |
} |
60 | |
} |
61 | |
|
62 | 5 | boolean reverse = false; |
63 | |
|
64 | 5 | if ( jdk.startsWith( "!" ) ) |
65 | |
{ |
66 | 2 | reverse = true; |
67 | 2 | jdk = jdk.substring( 1 ); |
68 | |
} |
69 | |
|
70 | 5 | if ( getJdkVersion().startsWith( jdk ) ) |
71 | |
{ |
72 | 2 | return !reverse; |
73 | |
} |
74 | |
else |
75 | |
{ |
76 | 3 | return reverse; |
77 | |
} |
78 | |
} |
79 | |
|
80 | |
private boolean matchJdkVersionRange( String jdk ) |
81 | |
throws InvalidVersionSpecificationException |
82 | |
{ |
83 | 3 | VersionRange jdkVersionRange = VersionRange.createFromVersionSpec( convertJdkToMavenVersion( jdk ) ); |
84 | 2 | DefaultArtifactVersion jdkVersion = new DefaultArtifactVersion( convertJdkToMavenVersion( getJdkVersion() ) ); |
85 | 2 | return jdkVersionRange.containsVersion( jdkVersion ); |
86 | |
} |
87 | |
|
88 | |
private String convertJdkToMavenVersion( String jdk ) |
89 | |
{ |
90 | 5 | return jdk.replaceAll( "_", "-" ); |
91 | |
} |
92 | |
|
93 | |
protected String getJdkVersion() |
94 | |
{ |
95 | 1 | return JDK_VERSION; |
96 | |
} |
97 | |
|
98 | |
protected boolean canDetectActivation( Profile profile ) |
99 | |
{ |
100 | 121 | return profile.getActivation() != null && StringUtils.isNotEmpty( profile.getActivation().getJdk() ); |
101 | |
} |
102 | |
|
103 | |
} |