View Javadoc
1   /*
2    * Copyright (c) 2012-2023, jcabi.com
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met: 1) Redistributions of source code must retain the above
8    * copyright notice, this list of conditions and the following
9    * disclaimer. 2) Redistributions in binary form must reproduce the above
10   * copyright notice, this list of conditions and the following
11   * disclaimer in the documentation and/or other materials provided
12   * with the distribution. 3) Neither the name of the jcabi.com nor
13   * the names of its contributors may be used to endorse or promote
14   * products derived from this software without specific prior written
15   * permission.
16   *
17   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
19   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21   * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28   * OF THE POSSIBILITY OF SUCH DAMAGE.
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   * Classify current platform.
47   *
48   * @since 0.1
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       * Maven project.
60       */
61      @Parameter(
62          defaultValue = "${project}",
63          required = true,
64          readonly = true
65      )
66      private transient MavenProject project;
67  
68      /**
69       * Classifier to set.
70       *
71       * <p>This is the name of the Maven property to set, for example
72       * "mysql.classifier".</p>
73       */
74      @Parameter(defaultValue = "mysql.classifier", required = true)
75      private transient String classifier;
76  
77      /**
78       * Classification mappings.
79       *
80       * <p>This may be useful when your platform doesn't detect correctly (it may happen).
81       * In this case you may either specify the classifier explicitly for the
82       * maven-dependency-plugin, or use this mechanism of mappings. Each mapping
83       * must be formatted as "from to" (with a space inside),
84       * for example "linux-i386 linux-x86". When the platform
85       * is detected as "linux-i386", it will be changed to "linux-x86". You can specify
86       * multiple mappings, for example:</p>
87       *
88       * <pre>
89       * &lt;configuration&gt;
90       *   &lt;mappings&gt;
91       *     &lt;mapping&gt;linux-i386 linux-x86&lt;/mapping&gt;
92       *     &lt;mapping&gt;linux-amd64 linux-x86_64&lt;/mapping&gt;
93       *   &lt;/mappings&gt;
94       * &lt;/configuration&gt;
95       * </pre>
96       *
97       * <p>By default, the following mapping is used:
98       *
99       * <pre>
100      * &lt;configuration&gt;
101      *   &lt;mappings&gt;
102      *     &lt;mapping&gt;linux-i386 linux-x86&lt;/mapping&gt;
103      *   &lt;/mappings&gt;
104      * &lt;/configuration&gt;
105      * </pre>
106      *
107      * <p>It means that if your platform is detected as "i386", it will be
108      * changed to "x86".
109      *
110      * @since 0.9.0
111      * @checkstyle MemberNameCheck (5 lines)
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                     // @checkstyle LineLength (1 line)
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      * Set project.
143      * @param prj Project to set
144      */
145     public void setProject(final MavenProject prj) {
146         this.project = prj;
147     }
148 
149     /**
150      * Set classifier.
151      * @param name Name of property
152      */
153     public void setClassifier(final String name) {
154         this.classifier = name;
155     }
156 
157     /**
158      * Calculate value.
159      * @return Value to set
160      * @throws MojoFailureException If fails
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 }