1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 package com.jcabi.mysql.maven.plugin;
31
32 import com.jcabi.log.Logger;
33 import java.util.LinkedList;
34 import java.util.List;
35 import java.util.Locale;
36 import lombok.EqualsAndHashCode;
37 import lombok.ToString;
38 import org.apache.maven.plugin.AbstractMojo;
39 import org.apache.maven.plugin.MojoFailureException;
40 import org.apache.maven.plugins.annotations.LifecyclePhase;
41 import org.apache.maven.plugins.annotations.Mojo;
42 import org.apache.maven.plugins.annotations.Parameter;
43 import org.apache.maven.project.MavenProject;
44
45
46
47
48
49
50 @ToString
51 @EqualsAndHashCode(callSuper = false)
52 @Mojo(
53 threadSafe = true, name = "classify",
54 defaultPhase = LifecyclePhase.INITIALIZE
55 )
56 public final class ClassifyMojo extends AbstractMojo {
57
58
59
60
61 @Parameter(
62 defaultValue = "${project}",
63 required = true,
64 readonly = true
65 )
66 private transient MavenProject project;
67
68
69
70
71
72
73
74 @Parameter(defaultValue = "mysql.classifier", required = true)
75 private transient String classifier;
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113 @Parameter(required = true)
114 @SuppressWarnings("PMD.ImmutableField")
115 private transient List<String> mappings;
116
117 @Override
118 public void execute() throws MojoFailureException {
119 final String existing = this.project.getProperties()
120 .getProperty(this.classifier);
121 if (existing == null) {
122 final String arch = this.arch();
123 this.project.getProperties().setProperty(this.classifier, arch);
124 Logger.info(this, "${%s} set to \"%s\"", this.classifier, arch);
125 } else if (existing.equals(this.arch())) {
126 Logger.info(
127 this, "${%s} already set to \"%s\"",
128 this.classifier, this.arch()
129 );
130 } else {
131 throw new MojoFailureException(
132 String.format(
133
134 "Maven property ${%s} already set to \"%s\", can't change to \"%s\"",
135 this.classifier, existing, this.arch()
136 )
137 );
138 }
139 }
140
141
142
143
144
145 public void setProject(final MavenProject prj) {
146 this.project = prj;
147 }
148
149
150
151
152
153 public void setClassifier(final String name) {
154 this.classifier = name;
155 }
156
157
158
159
160
161
162 private String arch() throws MojoFailureException {
163 if (this.mappings == null) {
164 this.mappings = new LinkedList<>();
165 this.mappings.add("linux-i386 linux-x86_64");
166 }
167 final String arch = System.getProperty("os.arch").toLowerCase(Locale.ENGLISH);
168 final String[] words = System.getProperty("os.name").split(" ");
169 String value = String.format(
170 "%s-%s", words[0].toLowerCase(Locale.ENGLISH), arch
171 );
172 for (final String mapping : this.mappings) {
173 final String[] pair = mapping.split(" ");
174 if (pair.length != 2) {
175 throw new MojoFailureException(
176 String.format(
177 "Invalid mapping \"%s\" (should be \"from to\")",
178 mapping
179 )
180 );
181 }
182 if (value.equals(pair[0])) {
183 value = pair[1];
184 Logger.info(
185 this, "Architecture \"%s\" changed to \"%s\"",
186 pair[0], pair[1]
187 );
188 }
189 }
190 return value;
191 }
192
193 }