1 /*
2 * Copyright (c) 2012-2022 Yegor Bugayenko
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.maven.plugin;
31
32 import com.jcabi.log.Logger;
33 import java.io.File;
34 import java.io.IOException;
35 import org.apache.commons.io.FileUtils;
36 import org.apache.maven.plugin.MojoFailureException;
37
38 /**
39 * Operations on the unwoven classes, like storing them in a separate
40 * location from the woven ones. Unwoven classes are classes which weren't
41 * yet weaved by the aspect weaver.
42 *
43 * @since 0.15
44 */
45 final class UnwovenClasses {
46
47 /**
48 * Directory where unwoven classes are saved.
49 */
50 private final transient File unwoven;
51
52 /**
53 * Output directory from where the classes are taken.
54 */
55 private final transient File classes;
56
57 /**
58 * Maven execution phase.
59 */
60 private final transient String phase;
61
62 /**
63 * Constructor.
64 * @param uwvn Dir where unwoven classes go
65 * @param cls Directory where the classes are found
66 * @param phs Maven execution phase
67 */
68 UnwovenClasses(final File uwvn, final File cls,
69 final String phs) {
70 this.unwoven = uwvn;
71 this.classes = cls;
72 this.phase = phs;
73 }
74
75 /**
76 * Perform the copy. Unwoven classes go in <b>unwoven</b> directory, while
77 * unwoven test classes go in <b>unwoven</b> + -test directory.
78 * @throws MojoFailureException If there is an IOException when
79 * copying the files
80 */
81 void copy() throws MojoFailureException {
82 if ("process-classes".equals(this.phase)) {
83 this.unwoven.mkdirs();
84 Logger.info(
85 this, "Unwoven classes will be copied to %s",
86 this.unwoven
87 );
88 UnwovenClasses.copyContents(this.classes, this.unwoven);
89 } else if ("process-test-classes".equals(this.phase)) {
90 final String suffix = "-test";
91 final File tests = new File(
92 this.unwoven.getPath().concat(suffix)
93 );
94 tests.mkdirs();
95 Logger.info(
96 this, "Unwoven test classes will be copied to %s",
97 tests
98 );
99 UnwovenClasses.copyContents(this.classes, tests);
100 }
101 }
102
103 /**
104 * Copies contents from one dir to another.
105 * @param from From directory
106 * @param dest Destination directory
107 * @throws MojoFailureException If something goes wrong while
108 * cleaning the destination director or copying files to it
109 */
110 private static void copyContents(
111 final File from, final File dest) throws MojoFailureException {
112 try {
113 FileUtils.cleanDirectory(dest);
114 FileUtils.copyDirectory(from, dest, false);
115 } catch (final IOException ex) {
116 throw new MojoFailureException(
117 String.format(
118 "Error when cleaning dest dir or when copying files: %s",
119 ex.getMessage()
120 ),
121 ex
122 );
123 }
124 }
125
126 }