├── .gitignore
├── LICENSE
├── README.md
├── launch
└── build_cdi-utils [clean install].launch
├── pom.xml
└── src
├── main
└── java
│ └── com
│ └── itemis
│ └── maven
│ └── plugins
│ └── cdi
│ ├── AbstractCDIMojo.java
│ ├── CDIMojoProcessingStep.java
│ ├── ExecutionContext.java
│ ├── annotations
│ ├── MojoInject.java
│ ├── MojoProduces.java
│ ├── ProcessingStep.java
│ └── RollbackOnError.java
│ ├── internal
│ ├── beans
│ │ ├── CdiBeanWrapper.java
│ │ └── CdiProducerBean.java
│ └── util
│ │ ├── CDIUtil.java
│ │ ├── MavenUtil.java
│ │ └── workflow
│ │ ├── ParallelWorkflowStep.java
│ │ ├── ProcessingWorkflow.java
│ │ ├── SimpleWorkflowStep.java
│ │ ├── WorkflowConstants.java
│ │ ├── WorkflowExecutor.java
│ │ ├── WorkflowStep.java
│ │ ├── WorkflowUtil.java
│ │ └── WorkflowValidator.java
│ └── logging
│ ├── Logger.java
│ └── MavenLogWrapper.java
└── test
├── java
└── com
│ └── itemis
│ └── maven
│ └── plugins
│ └── cdi
│ └── util
│ ├── WorkflowUtilTest.java
│ └── WorkflowValidatorTest.java
└── resources
└── workflows
├── invalid
├── try-finally-noFinallyBlock
├── try-finally-noTryBlockClosing
└── try-finally_noTryBlockOpening
├── parallel
├── parallel_data
├── sequential
├── sequential_data
├── sequential_qualifiers
├── try-finally
└── try-finally_complex
/.gitignore:
--------------------------------------------------------------------------------
1 | #maven excludes
2 | target/
3 |
4 | #eclipse excludes
5 | .classpath
6 | .project
7 | .settings/
8 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Eclipse Public License - v 1.0
2 |
3 | THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC
4 | LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM
5 | CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT.
6 |
7 | 1. DEFINITIONS
8 |
9 | "Contribution" means:
10 |
11 | a) in the case of the initial Contributor, the initial code and documentation
12 | distributed under this Agreement, and
13 | b) in the case of each subsequent Contributor:
14 | i) changes to the Program, and
15 | ii) additions to the Program;
16 |
17 | where such changes and/or additions to the Program originate from and are
18 | distributed by that particular Contributor. A Contribution 'originates'
19 | from a Contributor if it was added to the Program by such Contributor
20 | itself or anyone acting on such Contributor's behalf. Contributions do not
21 | include additions to the Program which: (i) are separate modules of
22 | software distributed in conjunction with the Program under their own
23 | license agreement, and (ii) are not derivative works of the Program.
24 |
25 | "Contributor" means any person or entity that distributes the Program.
26 |
27 | "Licensed Patents" mean patent claims licensable by a Contributor which are
28 | necessarily infringed by the use or sale of its Contribution alone or when
29 | combined with the Program.
30 |
31 | "Program" means the Contributions distributed in accordance with this
32 | Agreement.
33 |
34 | "Recipient" means anyone who receives the Program under this Agreement,
35 | including all Contributors.
36 |
37 | 2. GRANT OF RIGHTS
38 | a) Subject to the terms of this Agreement, each Contributor hereby grants
39 | Recipient a non-exclusive, worldwide, royalty-free copyright license to
40 | reproduce, prepare derivative works of, publicly display, publicly
41 | perform, distribute and sublicense the Contribution of such Contributor,
42 | if any, and such derivative works, in source code and object code form.
43 | b) Subject to the terms of this Agreement, each Contributor hereby grants
44 | Recipient a non-exclusive, worldwide, royalty-free patent license under
45 | Licensed Patents to make, use, sell, offer to sell, import and otherwise
46 | transfer the Contribution of such Contributor, if any, in source code and
47 | object code form. This patent license shall apply to the combination of
48 | the Contribution and the Program if, at the time the Contribution is
49 | added by the Contributor, such addition of the Contribution causes such
50 | combination to be covered by the Licensed Patents. The patent license
51 | shall not apply to any other combinations which include the Contribution.
52 | No hardware per se is licensed hereunder.
53 | c) Recipient understands that although each Contributor grants the licenses
54 | to its Contributions set forth herein, no assurances are provided by any
55 | Contributor that the Program does not infringe the patent or other
56 | intellectual property rights of any other entity. Each Contributor
57 | disclaims any liability to Recipient for claims brought by any other
58 | entity based on infringement of intellectual property rights or
59 | otherwise. As a condition to exercising the rights and licenses granted
60 | hereunder, each Recipient hereby assumes sole responsibility to secure
61 | any other intellectual property rights needed, if any. For example, if a
62 | third party patent license is required to allow Recipient to distribute
63 | the Program, it is Recipient's responsibility to acquire that license
64 | before distributing the Program.
65 | d) Each Contributor represents that to its knowledge it has sufficient
66 | copyright rights in its Contribution, if any, to grant the copyright
67 | license set forth in this Agreement.
68 |
69 | 3. REQUIREMENTS
70 |
71 | A Contributor may choose to distribute the Program in object code form under
72 | its own license agreement, provided that:
73 |
74 | a) it complies with the terms and conditions of this Agreement; and
75 | b) its license agreement:
76 | i) effectively disclaims on behalf of all Contributors all warranties
77 | and conditions, express and implied, including warranties or
78 | conditions of title and non-infringement, and implied warranties or
79 | conditions of merchantability and fitness for a particular purpose;
80 | ii) effectively excludes on behalf of all Contributors all liability for
81 | damages, including direct, indirect, special, incidental and
82 | consequential damages, such as lost profits;
83 | iii) states that any provisions which differ from this Agreement are
84 | offered by that Contributor alone and not by any other party; and
85 | iv) states that source code for the Program is available from such
86 | Contributor, and informs licensees how to obtain it in a reasonable
87 | manner on or through a medium customarily used for software exchange.
88 |
89 | When the Program is made available in source code form:
90 |
91 | a) it must be made available under this Agreement; and
92 | b) a copy of this Agreement must be included with each copy of the Program.
93 | Contributors may not remove or alter any copyright notices contained
94 | within the Program.
95 |
96 | Each Contributor must identify itself as the originator of its Contribution,
97 | if
98 | any, in a manner that reasonably allows subsequent Recipients to identify the
99 | originator of the Contribution.
100 |
101 | 4. COMMERCIAL DISTRIBUTION
102 |
103 | Commercial distributors of software may accept certain responsibilities with
104 | respect to end users, business partners and the like. While this license is
105 | intended to facilitate the commercial use of the Program, the Contributor who
106 | includes the Program in a commercial product offering should do so in a manner
107 | which does not create potential liability for other Contributors. Therefore,
108 | if a Contributor includes the Program in a commercial product offering, such
109 | Contributor ("Commercial Contributor") hereby agrees to defend and indemnify
110 | every other Contributor ("Indemnified Contributor") against any losses,
111 | damages and costs (collectively "Losses") arising from claims, lawsuits and
112 | other legal actions brought by a third party against the Indemnified
113 | Contributor to the extent caused by the acts or omissions of such Commercial
114 | Contributor in connection with its distribution of the Program in a commercial
115 | product offering. The obligations in this section do not apply to any claims
116 | or Losses relating to any actual or alleged intellectual property
117 | infringement. In order to qualify, an Indemnified Contributor must:
118 | a) promptly notify the Commercial Contributor in writing of such claim, and
119 | b) allow the Commercial Contributor to control, and cooperate with the
120 | Commercial Contributor in, the defense and any related settlement
121 | negotiations. The Indemnified Contributor may participate in any such claim at
122 | its own expense.
123 |
124 | For example, a Contributor might include the Program in a commercial product
125 | offering, Product X. That Contributor is then a Commercial Contributor. If
126 | that Commercial Contributor then makes performance claims, or offers
127 | warranties related to Product X, those performance claims and warranties are
128 | such Commercial Contributor's responsibility alone. Under this section, the
129 | Commercial Contributor would have to defend claims against the other
130 | Contributors related to those performance claims and warranties, and if a
131 | court requires any other Contributor to pay any damages as a result, the
132 | Commercial Contributor must pay those damages.
133 |
134 | 5. NO WARRANTY
135 |
136 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED ON AN
137 | "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR
138 | IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR CONDITIONS OF TITLE,
139 | NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Each
140 | Recipient is solely responsible for determining the appropriateness of using
141 | and distributing the Program and assumes all risks associated with its
142 | exercise of rights under this Agreement , including but not limited to the
143 | risks and costs of program errors, compliance with applicable laws, damage to
144 | or loss of data, programs or equipment, and unavailability or interruption of
145 | operations.
146 |
147 | 6. DISCLAIMER OF LIABILITY
148 |
149 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR ANY
150 | CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL,
151 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION
152 | LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
153 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
154 | ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE
155 | EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY
156 | OF SUCH DAMAGES.
157 |
158 | 7. GENERAL
159 |
160 | If any provision of this Agreement is invalid or unenforceable under
161 | applicable law, it shall not affect the validity or enforceability of the
162 | remainder of the terms of this Agreement, and without further action by the
163 | parties hereto, such provision shall be reformed to the minimum extent
164 | necessary to make such provision valid and enforceable.
165 |
166 | If Recipient institutes patent litigation against any entity (including a
167 | cross-claim or counterclaim in a lawsuit) alleging that the Program itself
168 | (excluding combinations of the Program with other software or hardware)
169 | infringes such Recipient's patent(s), then such Recipient's rights granted
170 | under Section 2(b) shall terminate as of the date such litigation is filed.
171 |
172 | All Recipient's rights under this Agreement shall terminate if it fails to
173 | comply with any of the material terms or conditions of this Agreement and does
174 | not cure such failure in a reasonable period of time after becoming aware of
175 | such noncompliance. If all Recipient's rights under this Agreement terminate,
176 | Recipient agrees to cease use and distribution of the Program as soon as
177 | reasonably practicable. However, Recipient's obligations under this Agreement
178 | and any licenses granted by Recipient relating to the Program shall continue
179 | and survive.
180 |
181 | Everyone is permitted to copy and distribute copies of this Agreement, but in
182 | order to avoid inconsistency the Agreement is copyrighted and may only be
183 | modified in the following manner. The Agreement Steward reserves the right to
184 | publish new versions (including revisions) of this Agreement from time to
185 | time. No one other than the Agreement Steward has the right to modify this
186 | Agreement. The Eclipse Foundation is the initial Agreement Steward. The
187 | Eclipse Foundation may assign the responsibility to serve as the Agreement
188 | Steward to a suitable separate entity. Each new version of the Agreement will
189 | be given a distinguishing version number. The Program (including
190 | Contributions) may always be distributed subject to the version of the
191 | Agreement under which it was received. In addition, after a new version of the
192 | Agreement is published, Contributor may elect to distribute the Program
193 | (including its Contributions) under the new version. Except as expressly
194 | stated in Sections 2(a) and 2(b) above, Recipient receives no rights or
195 | licenses to the intellectual property of any Contributor under this Agreement,
196 | whether expressly, by implication, estoppel or otherwise. All rights in the
197 | Program not expressly granted under this Agreement are reserved.
198 |
199 | This Agreement is governed by the laws of the State of New York and the
200 | intellectual property laws of the United States of America. No party to this
201 | Agreement will bring a legal action under this Agreement more than one year
202 | after the cause of action arose. Each party waives its rights to a jury trial in
203 | any resulting litigation.
204 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | CDI-based Dependency Injection for Maven Plugin Development
2 | ===========================================================
3 | [](https://maven-badges.herokuapp.com/maven-central/com.itemis.maven.plugins/cdi-plugin-utils)
4 |
5 | This small library enables the usage of CDI-based dependency injection in Apache Maven plugins which changes the way of implementing Maven plugins fundamentally.
6 |
7 |
8 | Requirements
9 | ------------
10 | * JDK 1.7 or higher
11 | * Apache Maven 3.x
12 |
13 |
14 | The Idea Behind It
15 | ------------------
16 | The explicit desire for dependency injection (DI) in Maven plugins came up during the development of the [Unleash Maven Plugin](https://github.com/shillner/unleash-maven-plugin/). There it was necessary to dynamically inject an implementation of the ScmProvider interface into the provider registry that isn't even known at compile time. A second wish was to simply add those implementations as plugin dependencies when configuring the plugin for your project.
17 |
18 | Since an examination of Maven's DI capabilities did not yield any satisfactory results, this project was brought to life. First there was only the need to enable DI for Maven plugins, without any technological preference. But fast the requirements became more concrete and the choice fell on CDI based on its reference implementation Weld. Here are some of the core requirements that led to the current concepts and implementation:
19 |
20 | * DI in general to decouple components
21 | * Classpath-based autowiring of components
22 | * Distributed feature implementation without having to grind parameters through hundreds of classes
23 | * Ensuring extensibility of the plugins based on this library
24 | * Safe execution of the various processing steps of the plugin with implicit rollback in case of an error
25 |
26 |
27 | The Core Concepts
28 | -----------------
29 | * CDI-based dependency injection implemented using [Weld SE](https://docs.jboss.org/weld/reference/latest/en-US/html/environments.html#_java_se)
30 | * @Inject
31 | * Qualifiers, Alternatives
32 | * Producers
33 | * @PostConstruct, @PreDestroy
34 | * Events
35 | * Classpath-based autowiring
36 | * The plugin's classpath is automatically scanned for beans (plugin and plugin dependencies)
37 | * No need for declaring beans in any kind of descriptor or manually bind or wire beans
38 | * Workflow-based architecture
39 | * Plugins define a default workflow for each Mojo
40 | * Workflow consisting of several processing steps which results in much smaller and clearer feature implementations
41 | * Safe workflow processing
42 | * The plugin processes the workflow step by step
43 | * Steps can implement one or more rollback methods that are called under certain circumstances
44 | * If any workflow step fails with an exception all processed steps are rolled-back in their reverse order
45 | * Each step only needs to rollback its own changes
46 | * Extensibility by design
47 | * Classpath scanning enables you to add more processing steps or other implementations to the plugin dependencies
48 | * Overriding of the default workflow of a Mojo makes it possible to redefine the workflow, f.i. when embedding new steps
49 |
50 |
51 | Further Information
52 | -------------------
53 | For more detailed information about how to implement Maven plugins using this library please refer to the [Project Wiki](https://github.com/shillner/maven-cdi-plugin-utils/wiki). There all concepts and their implementation as well as the general usage is explained in detail.
54 |
55 | A reference plugin that bases on this library is available here: [Unleash Maven Plugin](https://github.com/shillner/unleash-maven-plugin/)
56 | This plugin provides f.i. an SCM provider API that is implemented in several external projects such as [Unleash SCM Provider for Git](https://github.com/shillner/unleash-scm-provider-git). These provider implementations can then be added to the plugin dependencies in order to support other SCM types during processing.
57 |
58 | A further project is available here: [Maven CDI Processing hooks](https://github.com/shillner/maven-cdi-plugin-hooks)
59 | This project provides some additional processing step implementations that can be used to extend processing workflows by simply adding the library to the plugin dependencies and overriding the processing workflow.
60 |
--------------------------------------------------------------------------------
/launch/build_cdi-utils [clean install].launch:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 | 4.0.0
3 |
4 |
5 | com.itemis
6 | org-parent
7 | 1
8 |
9 |
10 | com.itemis.maven.plugins
11 | cdi-plugin-utils
12 | 3.4.1-SNAPSHOT
13 |
14 | CDI Plugin Utilities
15 | Provides an abstract Mojo that enables CDI-based dependency injection for Maven Plugins.
16 | https://github.com/shillner/maven-cdi-plugin-utils
17 | 2016
18 |
19 |
20 |
21 | shillner
22 | Stanley Hillner
23 | itemis AG
24 | https://itemis.com/
25 | 1
26 |
27 |
28 |
29 |
30 | scm:git:https://github.com/shillner/maven-cdi-plugin-utils.git
31 | https://github.com/shillner/maven-cdi-plugin-utils
32 | HEAD
33 |
34 |
35 |
36 | GitHub
37 | https://github.com/shillner/maven-cdi-plugin-utils/issues
38 |
39 |
40 |
41 | 3.2.1
42 |
43 |
44 |
45 | 1.0.2.v20150114
46 | 0.2.5
47 | 1.2
48 | 19.0
49 | 1.7
50 | 1
51 | 4.12
52 | 1.10.3
53 | 3.2.1
54 | 3.4
55 | 1.7.21
56 | 2.3.3.Final
57 |
58 |
59 |
60 |
61 | org.eclipse.aether
62 | aether-api
63 | ${version.aether}
64 |
65 |
66 | org.eclipse.aether
67 | aether-impl
68 | ${version.aether}
69 |
70 |
71 | de.vandermeer
72 | asciitable
73 | ${version.asciitable}
74 |
75 |
76 | javax.enterprise
77 | cdi-api
78 | ${version.cdi-api}
79 |
80 |
81 | com.google.guava
82 | guava
83 | ${version.guava}
84 |
85 |
86 | javax.inject
87 | javax.inject
88 | ${version.javax.inject}
89 |
90 |
91 | junit
92 | junit
93 | ${version.junit}
94 |
95 |
96 | com.tngtech.java
97 | junit-dataprovider
98 | ${version.junit-dataprovider}
99 | test
100 |
101 |
102 | org.apache.maven
103 | maven-core
104 | ${version.maven}
105 |
106 |
107 | org.apache.maven
108 | maven-model
109 | ${version.maven}
110 |
111 |
112 | org.apache.maven.plugin-tools
113 | maven-plugin-annotations
114 | ${version.maven-plugin-plugin}
115 |
116 |
117 | org.apache.maven
118 | maven-plugin-api
119 | ${version.maven}
120 |
121 |
122 | org.apache.maven
123 | maven-settings
124 | ${version.maven}
125 |
126 |
127 | org.slf4j
128 | slf4j-simple
129 | ${version.slf4j-simple}
130 |
131 |
132 | org.jboss.weld.se
133 | weld-se
134 | ${version.weld-se}
135 |
136 |
137 |
138 |
139 |
140 |
141 |
142 | org.apache.maven.plugins
143 | maven-compiler-plugin
144 |
145 | ${version.java}
146 | ${version.java}
147 |
148 |
149 |
150 |
151 |
152 |
153 |
154 |
155 | disable-java8-doclint
156 |
157 | [1.8,)
158 |
159 |
160 |
161 |
162 |
163 | org.apache.maven.plugins
164 | maven-javadoc-plugin
165 |
166 | -Xdoclint:none
167 |
168 |
169 |
170 |
171 |
172 |
173 |
174 |
--------------------------------------------------------------------------------
/src/main/java/com/itemis/maven/plugins/cdi/AbstractCDIMojo.java:
--------------------------------------------------------------------------------
1 | package com.itemis.maven.plugins.cdi;
2 |
3 | import java.io.File;
4 | import java.io.InputStream;
5 | import java.lang.reflect.Field;
6 | import java.lang.reflect.Method;
7 | import java.util.Collection;
8 | import java.util.List;
9 | import java.util.Map;
10 | import java.util.Set;
11 |
12 | import javax.enterprise.event.Observes;
13 | import javax.enterprise.inject.spi.AfterBeanDiscovery;
14 | import javax.enterprise.inject.spi.BeanManager;
15 | import javax.enterprise.inject.spi.Extension;
16 | import javax.enterprise.inject.spi.ProcessAnnotatedType;
17 | import javax.inject.Named;
18 |
19 | import org.apache.maven.execution.MavenSession;
20 | import org.apache.maven.model.Dependency;
21 | import org.apache.maven.plugin.AbstractMojo;
22 | import org.apache.maven.plugin.MojoExecution;
23 | import org.apache.maven.plugin.MojoExecutionException;
24 | import org.apache.maven.plugin.MojoFailureException;
25 | import org.apache.maven.plugin.PluginParameterExpressionEvaluator;
26 | import org.apache.maven.plugin.descriptor.MojoDescriptor;
27 | import org.apache.maven.plugin.descriptor.PluginDescriptor;
28 | import org.apache.maven.plugins.annotations.Component;
29 | import org.apache.maven.plugins.annotations.Parameter;
30 | import org.apache.maven.project.MavenProject;
31 | import org.apache.maven.settings.Settings;
32 | import org.eclipse.aether.RepositorySystemSession;
33 | import org.eclipse.aether.impl.ArtifactResolver;
34 | import org.eclipse.aether.repository.RemoteRepository;
35 | import org.jboss.weld.environment.se.Weld;
36 | import org.jboss.weld.environment.se.WeldContainer;
37 |
38 | import com.google.common.base.Optional;
39 | import com.google.common.base.Preconditions;
40 | import com.google.common.collect.Maps;
41 | import com.google.common.collect.Sets;
42 | import com.itemis.maven.plugins.cdi.annotations.MojoProduces;
43 | import com.itemis.maven.plugins.cdi.annotations.ProcessingStep;
44 | import com.itemis.maven.plugins.cdi.internal.beans.CdiBeanWrapper;
45 | import com.itemis.maven.plugins.cdi.internal.beans.CdiProducerBean;
46 | import com.itemis.maven.plugins.cdi.internal.util.CDIUtil;
47 | import com.itemis.maven.plugins.cdi.internal.util.MavenUtil;
48 | import com.itemis.maven.plugins.cdi.internal.util.workflow.ProcessingWorkflow;
49 | import com.itemis.maven.plugins.cdi.internal.util.workflow.WorkflowExecutor;
50 | import com.itemis.maven.plugins.cdi.internal.util.workflow.WorkflowUtil;
51 | import com.itemis.maven.plugins.cdi.internal.util.workflow.WorkflowValidator;
52 | import com.itemis.maven.plugins.cdi.logging.MavenLogWrapper;
53 |
54 | /**
55 | * An abstract Mojo that enables CDI-based dependency injection for the current maven plugin.
56 | * This Mojo enables you to decouple different parts of your plugin implementation and also dynamically inject
57 | * additional funktionality into your plugin.
58 | *
59 | *
60 | * ATTENTION: Please do not use annotations such as {@code @javax.inject.Inject} or
61 | * {@code @javax.enterprise.inject.Produces} directly in your Mojo! There are special replacements for that in the
62 | * annotations package of this library. Using CDI annotations directly in the Mojo would trigger Maven's own CDI
63 | * adaption!
64 | *
65 | *
66 | * Using this abstract Mojo as the parent of your own Mojo, you can simply see the Mojo class as a data container whose
67 | * single responsibility is to provide parameters for your business logic implementations. Simply get the
68 | * Mojo parameters injected and use the producer annotation to provide the bean to your implementations:
69 | *
70 | *
94 | *
95 | * ATTENTION: Make sure to not override the {@link #execute()} method since this method is responsible for the
96 | * CDI setup and will
97 | * trigger your business logic impelementations automatically.
98 | * Implement your business logic in one or more classes that are annotated with {@link ProcessingStep} and implement
99 | * {@link CDIMojoProcessingStep}. Then orchestrate your standard business workflow in a worflow descriptor file.
100 | *
101 | *
102 | *
The Workflow Descriptor
103 | *
104 | *
The descriptor is located under META-INF/workflows
105 | *
The name of the workflow descriptor file must match the name of the goal. F.i. goal="perform"
106 | * workflow-file="META-INF/workflows/perform"
107 | *
A simple workflow lists just all processing step ids in the respective order (each id on a new line).
108 | *
Steps that are encapsuled in parallel{} are executed in parallel. All other steps will be executed
109 | * sequentially.
110 | *
A line starting with a # will be treated as a comment.
118 | * init
119 | * # The following steps can be run in parallel since they do not modify the project but only perform some checks
120 | * parallel {
121 | * checkUser
122 | * checkConnection
123 | * checkAether
124 | * }
125 | * compute
126 | * upload
127 | * validate
128 | *
129 | *
130 | * @author Stanley Hillner
131 | * @since 1.0.0
132 | */
133 | public class AbstractCDIMojo extends AbstractMojo implements Extension {
134 | private static final String SYSPROP_PRINT_WF = "printWorkflow";
135 | private static final String SYSPROP_PRINT_STEPS = "printSteps";
136 |
137 | @Component
138 | private ArtifactResolver _resolver;
139 |
140 | @Parameter(defaultValue = "${settings}", readonly = true, required = true)
141 | private Settings _settings;
142 |
143 | @Parameter(readonly = true, defaultValue = "${repositorySystemSession}")
144 | private RepositorySystemSession _repoSystemSession;
145 |
146 | @Parameter(readonly = true, defaultValue = "${project.remotePluginRepositories}")
147 | private List _pluginRepos;
148 |
149 | @Parameter(property = "mojoExecution", readonly = true)
150 | private MojoExecution _mojoExecution;
151 |
152 | @Parameter(property = "session", readonly = true)
153 | private MavenSession _session;
154 |
155 | @Parameter(property = "workflow")
156 | private File workflowDescriptor;
157 |
158 | @Parameter(defaultValue = "true", property = "enableLogTimestamps")
159 | @MojoProduces
160 | @Named("enableLogTimestamps")
161 | private boolean enableLogTimestamps;
162 |
163 | private ProcessingWorkflow workflow;
164 |
165 | private Map allAvailableProcessingSteps = Maps.newHashMap();
166 |
167 | @MojoProduces
168 | public final MavenLogWrapper createLogWrapper() {
169 | MavenLogWrapper log = new MavenLogWrapper(getLog());
170 | if (this.enableLogTimestamps) {
171 | log.enableLogTimestamps();
172 | }
173 | return log;
174 | }
175 |
176 | @Override
177 | public final void execute() throws MojoExecutionException, MojoFailureException {
178 | if (System.getProperty(SYSPROP_PRINT_WF) != null) {
179 | WorkflowUtil.printWorkflow(getGoalName(), getPluginDescriptor(), Optional.fromNullable(this.workflowDescriptor),
180 | createLogWrapper());
181 | return;
182 | }
183 |
184 | System.setProperty("org.jboss.logging.provider", "slf4j");
185 | String logLevel = "info";
186 | if (getLog().isDebugEnabled()) {
187 | logLevel = "debug";
188 | }
189 | System.setProperty("org.slf4j.simpleLogger.log.org.jboss.weld", logLevel);
190 |
191 | Weld weld = new Weld();
192 | weld.addExtension(this);
193 | addPluginDependencies(weld);
194 | WeldContainer weldContainer = null;
195 | try {
196 | weldContainer = weld.initialize();
197 | if (System.getProperty(SYSPROP_PRINT_STEPS) != null) {
198 | WorkflowUtil.printAvailableSteps(this.allAvailableProcessingSteps, createLogWrapper());
199 | return;
200 | }
201 |
202 | WorkflowUtil.addExecutionContexts(getWorkflow());
203 | Map processingSteps = getAllProcessingSteps(weldContainer);
204 |
205 | PluginParameterExpressionEvaluator expressionEvaluator = new PluginParameterExpressionEvaluator(this._session,
206 | this._mojoExecution);
207 | WorkflowExecutor executor = new WorkflowExecutor(getWorkflow(), processingSteps, getLog(), expressionEvaluator);
208 | executor.validate(!this._settings.isOffline());
209 | executor.execute();
210 | } finally {
211 | if (weldContainer != null && weldContainer.isRunning()) {
212 | weldContainer.shutdown();
213 | }
214 | }
215 | }
216 |
217 | private ProcessingWorkflow getWorkflow() throws MojoExecutionException, MojoFailureException {
218 | if (this.workflow == null) {
219 | InputStream wfDescriptor = WorkflowUtil.getWorkflowDescriptor(getGoalName(), getPluginDescriptor(),
220 | Optional.fromNullable(this.workflowDescriptor), createLogWrapper());
221 |
222 | try {
223 | WorkflowValidator.validateSyntactically(wfDescriptor);
224 | } catch (RuntimeException e) {
225 | throw new MojoFailureException(e.getMessage());
226 | }
227 |
228 | wfDescriptor = WorkflowUtil.getWorkflowDescriptor(getGoalName(), getPluginDescriptor(),
229 | Optional.fromNullable(this.workflowDescriptor), createLogWrapper());
230 | this.workflow = WorkflowUtil.parseWorkflow(wfDescriptor, getGoalName());
231 | }
232 | return this.workflow;
233 | }
234 |
235 | @SuppressWarnings("unused")
236 | // will be called automatically by the CDI container for all annotated types
237 | private void skipUnusedStepsFromBeanDiscovery(@Observes ProcessAnnotatedType> event, BeanManager beanManager)
238 | throws MojoExecutionException, MojoFailureException {
239 | // https://github.com/shillner/maven-cdi-plugin-utils/issues/14
240 | Class> type = event.getAnnotatedType().getJavaClass();
241 | ProcessingStep annotation = type.getAnnotation(ProcessingStep.class);
242 | if (annotation != null) {
243 | // adding the step to the list of all available processing steps
244 | String id = annotation.id();
245 | Preconditions.checkState(!this.allAvailableProcessingSteps.containsKey(id),
246 | "The processing step id '" + id + "' is not unique!");
247 | this.allAvailableProcessingSteps.put(id, annotation);
248 |
249 | // vetoing the bean discovery of a step that is not part of the current workflow
250 | // this prevents the issue that data shall be injected that isn't produced anywhere!
251 | ProcessingWorkflow workflow = getWorkflow();
252 | if (!workflow.containsStep(annotation.id())) {
253 | event.veto();
254 | }
255 | }
256 | }
257 |
258 | @SuppressWarnings("unused")
259 | // will be called automatically by the CDI container once the bean discovery has finished
260 | private void processMojoCdiProducerFields(@Observes AfterBeanDiscovery event, BeanManager beanManager)
261 | throws MojoExecutionException {
262 |
263 | Class> cls = getClass();
264 | Set fields = Sets.newHashSet();
265 |
266 | while (cls != AbstractCDIMojo.class) {
267 | fields.addAll(Sets.newHashSet(cls.getFields()));
268 | fields.addAll(Sets.newHashSet(cls.getDeclaredFields()));
269 | cls = cls.getSuperclass();
270 | }
271 |
272 | for (Field f : fields) {
273 | if (f.isAnnotationPresent(MojoProduces.class)) {
274 | try {
275 | f.setAccessible(true);
276 | event.addBean(
277 | new CdiBeanWrapper