001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.axis2.maven2.wsdl2code;
020:
021: import org.apache.axis2.util.CommandLineOption;
022: import org.apache.axis2.util.CommandLineOptionConstants;
023: import org.apache.axis2.util.CommandLineOptionParser;
024: import org.apache.axis2.wsdl.codegen.CodeGenerationEngine;
025: import org.apache.axis2.wsdl.codegen.CodeGenerationException;
026: import org.apache.maven.artifact.Artifact;
027: import org.apache.maven.artifact.factory.ArtifactFactory;
028: import org.apache.maven.plugin.AbstractMojo;
029: import org.apache.maven.plugin.MojoExecutionException;
030: import org.apache.maven.plugin.MojoFailureException;
031: import org.apache.maven.plugin.logging.Log;
032: import org.apache.maven.project.MavenProject;
033:
034: import java.io.File;
035: import java.util.ArrayList;
036: import java.util.Arrays;
037: import java.util.Collection;
038: import java.util.HashMap;
039: import java.util.HashSet;
040: import java.util.Iterator;
041: import java.util.List;
042: import java.util.Map;
043: import java.util.Set;
044: import java.util.Properties;
045:
046: /**
047: * @description A Mojo for generating Java sources from a WSDL.
048: * @goal wsdl2code
049: * @phase generate-sources
050: * @requiresDependencyResolution test
051: */
052: public class WSDL2CodeMojo extends AbstractMojo {
053: /**
054: * The maven project.
055: *
056: * @parameter expression="${project}"
057: * @read-only
058: * @required
059: */
060: private MavenProject project;
061:
062: /**
063: * The artifact factory.
064: *
065: * @parameter expression="${component.org.apache.maven.artifact.factory.ArtifactFactory}"
066: * @read-only
067: * @required
068: */
069: private ArtifactFactory artifactFactory;
070:
071: /**
072: * The plugins artifact list.
073: *
074: * @parameter expression="${plugin.artifacts}"
075: * @read-only
076: * @required
077: */
078: private List pluginArtifacts;
079:
080: /**
081: * The WSDL file, which is being read.
082: *
083: * @parameter expression="${axis2.wsdl2code.wsdl}" default-value="src/main/axis2/service.wsdl"
084: */
085: private File wsdlFile;
086:
087: /**
088: * The output directory, where the generated sources are being created.
089: *
090: * @parameter expression="${axis2.wsdl2code.target}" default-value="${project.build.directory}/generated-sources/axis2/wsdl2code"
091: */
092: private File outputDirectory;
093:
094: /**
095: * Package name of the generated sources; will be used to create a package structure below the
096: * output directory.
097: *
098: * @parameter expression="${axis2.wsdl2code.package}"
099: * @required
100: */
101: private String packageName;
102:
103: /**
104: * The programming language of the generated sources.
105: *
106: * @parameter expression="${axis2.wsdl2code.language}" default-value="java"
107: */
108: private String language;
109:
110: /**
111: * The databinding framework, which is being used.
112: *
113: * @parameter expression="${axis2.wsdl2code.databindingName}" default-value="adb"
114: */
115: private String databindingName;
116:
117: /**
118: * Port name, for which to generate sources. By default, sources will be generated for all
119: * ports.
120: *
121: * @parameter expression="${axis2.wsdl2code.portName}"
122: */
123: private String portName;
124:
125: /**
126: * Service name, for which to generate sources. By default, sources will be generated for all
127: * services.
128: *
129: * @parameter expression="${axis2.wsdl2code.serviceName}"
130: */
131: private String serviceName;
132:
133: /**
134: * Mode, for which sources are being generated; either of "sync", "async" or "both".
135: *
136: * @parameter expression="${axis2.wsdl2code.syncMode}" default-value="both"
137: */
138: private String syncMode;
139:
140: /**
141: * Whether server side sources are being generated.
142: *
143: * @parameter expression="${axis2.wsdl2code.generateServerSide}" default-value="false"
144: */
145: private boolean generateServerSide;
146:
147: /**
148: * Whether to generate sources for a test case.
149: *
150: * @parameter expression="${axis2.wsdl2code.generateTestCase}" default-value="false"
151: */
152: private boolean generateTestcase;
153:
154: /**
155: * Whether to generate a "services.xml" file.
156: *
157: * @parameter expression="${axis2.wsdl2code.generateServicesXml}" default-value="false"
158: */
159: private boolean generateServicesXml;
160:
161: /**
162: * Whether to generate simply all classes. This is only valid in conjunction with
163: * "generateServerSide".
164: *
165: * @parameter expression="${axis2.wsdl2code.generateAllClasses}" default-value="false"
166: */
167: private boolean generateAllClasses;
168:
169: /**
170: * Whether to unpack classes.
171: *
172: * @parameter expression="${axis2.wsdl2code.unpackClasses}" default-value="false"
173: */
174: private boolean unpackClasses;
175:
176: /**
177: * Whether to generate the server side interface.
178: *
179: * @parameter expression="${axis2.wsdl2code.generateServerSideInterface}" default-value="false"
180: */
181: private boolean generateServerSideInterface = false;
182:
183: /**
184: * @parameter expression="${axis2.wsdl2code.repositoryPath}"
185: */
186: private String repositoryPath = null;
187:
188: /**
189: * @parameter expression="${axis2.wsdl2code.externalMapping}"
190: */
191: private String externalMapping = null;
192:
193: /**
194: * @parameter expression="${axis2.wsdl2code.wsdlVersion}"
195: */
196: private String wsdlVersion = null;
197:
198: /**
199: * @parameter expression="${axis2.wsdl2code.targetSourceFolderLocation}"
200: */
201: private String targetSourceFolderLocation = null;
202:
203: /**
204: * @parameter expression="${axis2.wsdl2code.targetResourcesFolderLocation}"
205: */
206: private String targetResourcesFolderLocation = null;
207:
208: /**
209: * @parameter expression="${axis2.wsdl2code.unwrap}" default-value="false" *
210: */
211: private boolean unwrap = false;
212:
213: /**
214: * @parameter expression="${axis2.wsdl2code.allPorts}" default-value="false" *
215: */
216: private boolean allPorts = false;
217:
218: /**
219: * @parameter expression="${axis2.wsdl2code.backwardCompatible}" default-value="false" *
220: */
221: private boolean backwardCompatible = false;
222:
223: /**
224: * @parameter expression="${axis2.wsdl2code.flattenFiles}" default-value="false" *
225: */
226: private boolean flattenFiles = false;
227:
228: /**
229: * @parameter expression="${axis2.wsdl2code.skipMessageReceiver}" default-value="false" *
230: */
231: private boolean skipMessageReceiver = false;
232:
233: /**
234: * @parameter expression="${axis2.wsdl2code.skipBuildXML}" default-value="false" *
235: */
236: private boolean skipBuildXML = false;
237:
238: /**
239: * @parameter expression="${axis2.wsdl2code.skipWSDL}" default-value="false" *
240: */
241: private boolean skipWSDL = false;
242:
243: /**
244: * @parameter expression="${axis2.wsdl2code.overWrite}" default-value="false" *
245: */
246: private boolean overWrite = false;
247:
248: /**
249: * @parameter expression="${axis2.wsdl2code.suppressPrefixes}" default-value="false" *
250: */
251: private boolean suppressPrefixes = false;
252:
253: /**
254: * Specify databinding specific extra options
255: *
256: * @parameter expression="${axis2.java2wsdl.options}"
257: */
258: private Properties options;
259:
260: /** @parameter expression="${axis2.wsdl2code.namespaceToPackages}" */
261: private String namespaceToPackages = null;
262:
263: /** @parameter */
264: private NamespaceURIMapping[] namespaceURIs = null;
265:
266: private static class InheritedArtifact {
267: private final String groupId, artifactId;
268: private boolean added;
269:
270: InheritedArtifact(String pGroupId, String pArtifactId) {
271: groupId = pGroupId;
272: artifactId = pArtifactId;
273: }
274:
275: String getGroupId() {
276: return groupId;
277: }
278:
279: String getArtifactId() {
280: return artifactId;
281: }
282:
283: boolean isAdded() {
284: return added;
285: }
286:
287: void setAdded() {
288: if (added) {
289: throw new IllegalStateException(
290: "This artifact was already added: " + groupId
291: + ":" + artifactId);
292: }
293: }
294: }
295:
296: private static final InheritedArtifact[] inheritedArtifacts = {
297: new InheritedArtifact("org.apache.ws.commons.axiom",
298: "axiom-api"),
299: new InheritedArtifact("org.apache.ws.commons.axiom",
300: "axiom-impl"),
301: new InheritedArtifact("org.apache.ws.commons", "neethi"),
302: new InheritedArtifact("wsdl4j", "wsdl4j"),
303: new InheritedArtifact("commons-httpclient",
304: "commons-httpclient") };
305:
306: private static final InheritedArtifact[] adbArtifacts = { new InheritedArtifact(
307: "org.apache.axis2", "axis2-adb") };
308:
309: private static final InheritedArtifact[] xbeanArtifacts = {
310: new InheritedArtifact("org.apache.axis2", "axis2-xmlbeans"),
311: new InheritedArtifact("xmlbeans", "xbean") };
312:
313: /** Fills the option map. This map is passed onto the code generation API to generate the code. */
314: private Map fillOptionMap() throws MojoFailureException {
315: Map optionMap = new HashMap();
316:
317: ////////////////////////////////////////////////////////////////
318: //WSDL file name
319: optionMap
320: .put(
321: CommandLineOptionConstants.WSDL2JavaConstants.WSDL_LOCATION_URI_OPTION,
322: new CommandLineOption(
323: CommandLineOptionConstants.WSDL2JavaConstants.WSDL_LOCATION_URI_OPTION,
324: getStringArray(wsdlFile.getPath())));
325: //output location
326: optionMap
327: .put(
328: CommandLineOptionConstants.WSDL2JavaConstants.OUTPUT_LOCATION_OPTION,
329: new CommandLineOption(
330: CommandLineOptionConstants.WSDL2JavaConstants.OUTPUT_LOCATION_OPTION,
331: getStringArray(outputDirectory
332: .getPath())));
333: //////////////////////////////////////////////////////////////////
334: // Databinding type
335: optionMap
336: .put(
337: CommandLineOptionConstants.WSDL2JavaConstants.DATA_BINDING_TYPE_OPTION,
338: new CommandLineOption(
339: CommandLineOptionConstants.WSDL2JavaConstants.DATA_BINDING_TYPE_OPTION,
340: getStringArray(databindingName)));
341:
342: if ("async".equals(syncMode)) {
343: // Async only option - forcing to generate async methods only
344: optionMap
345: .put(
346: CommandLineOptionConstants.WSDL2JavaConstants.CODEGEN_ASYNC_ONLY_OPTION,
347: new CommandLineOption(
348: CommandLineOptionConstants.WSDL2JavaConstants.CODEGEN_ASYNC_ONLY_OPTION,
349: new String[0]));
350: } else if ("sync".equals(syncMode)) {
351: // Sync only option - forcing to generate Sync methods only
352: optionMap
353: .put(
354: CommandLineOptionConstants.WSDL2JavaConstants.CODEGEN_SYNC_ONLY_OPTION,
355: new CommandLineOption(
356: CommandLineOptionConstants.WSDL2JavaConstants.CODEGEN_SYNC_ONLY_OPTION,
357: new String[0]));
358: } else if ("both".equals(syncMode)) {
359: // Do nothing
360: } else {
361: throw new MojoFailureException("Invalid syncMode: "
362: + syncMode
363: + ", expected either of 'sync', 'async' or 'both'.");
364: }
365:
366: //Package
367: optionMap
368: .put(
369: CommandLineOptionConstants.WSDL2JavaConstants.PACKAGE_OPTION,
370: new CommandLineOption(
371: CommandLineOptionConstants.WSDL2JavaConstants.PACKAGE_OPTION,
372: getStringArray(packageName)));
373:
374: //stub language
375: optionMap
376: .put(
377: CommandLineOptionConstants.WSDL2JavaConstants.STUB_LANGUAGE_OPTION,
378: new CommandLineOption(
379: CommandLineOptionConstants.WSDL2JavaConstants.STUB_LANGUAGE_OPTION,
380: getStringArray(language)));
381:
382: //server side and generate services.xml options
383: if (generateServerSide) {
384: optionMap
385: .put(
386: CommandLineOptionConstants.WSDL2JavaConstants.SERVER_SIDE_CODE_OPTION,
387: new CommandLineOption(
388: CommandLineOptionConstants.WSDL2JavaConstants.SERVER_SIDE_CODE_OPTION,
389: new String[0]));
390:
391: //services XML generation - effective only when specified as the server side
392: if (generateServicesXml) {
393: optionMap
394: .put(
395: CommandLineOptionConstants.WSDL2JavaConstants.GENERATE_SERVICE_DESCRIPTION_OPTION,
396: new CommandLineOption(
397: CommandLineOptionConstants.WSDL2JavaConstants.GENERATE_SERVICE_DESCRIPTION_OPTION,
398: new String[0]));
399: }
400: //generate all option - Only valid when generating serverside code
401: if (generateAllClasses) {
402: optionMap
403: .put(
404: CommandLineOptionConstants.WSDL2JavaConstants.GENERATE_ALL_OPTION,
405: new CommandLineOption(
406: CommandLineOptionConstants.WSDL2JavaConstants.GENERATE_ALL_OPTION,
407: new String[0]));
408: }
409:
410: }
411:
412: //generate the test case
413: if (generateTestcase) {
414: optionMap
415: .put(
416: CommandLineOptionConstants.WSDL2JavaConstants.GENERATE_TEST_CASE_OPTION,
417: new CommandLineOption(
418: CommandLineOptionConstants.WSDL2JavaConstants.GENERATE_TEST_CASE_OPTION,
419: new String[0]));
420: }
421:
422: //Unwrap classes option - this determines whether the generated classes are inside the stub/MR
423: //or gets generates as seperate classes
424: if (unpackClasses) {
425: optionMap
426: .put(
427: CommandLineOptionConstants.WSDL2JavaConstants.UNPACK_CLASSES_OPTION,
428: new CommandLineOption(
429: CommandLineOptionConstants.WSDL2JavaConstants.UNPACK_CLASSES_OPTION,
430: new String[0]));
431: }
432:
433: //server side interface option
434: if (generateServerSideInterface) {
435: optionMap
436: .put(
437: CommandLineOptionConstants.WSDL2JavaConstants.SERVER_SIDE_INTERFACE_OPTION,
438: new CommandLineOption(
439: CommandLineOptionConstants.WSDL2JavaConstants.SERVER_SIDE_INTERFACE_OPTION,
440: new String[0]));
441: }
442:
443: if (unwrap) {
444: optionMap
445: .put(
446: CommandLineOptionConstants.WSDL2JavaConstants.UNWRAP_PARAMETERS,
447: new CommandLineOption(
448: CommandLineOptionConstants.WSDL2JavaConstants.UNWRAP_PARAMETERS,
449: new String[0]));
450: }
451:
452: if (allPorts) {
453: optionMap
454: .put(
455: CommandLineOptionConstants.WSDL2JavaConstants.All_PORTS_OPTION,
456: new CommandLineOption(
457: CommandLineOptionConstants.WSDL2JavaConstants.All_PORTS_OPTION,
458: new String[0]));
459: }
460:
461: if (backwardCompatible) {
462: optionMap
463: .put(
464: CommandLineOptionConstants.WSDL2JavaConstants.BACKWORD_COMPATIBILITY_OPTION,
465: new CommandLineOption(
466: CommandLineOptionConstants.WSDL2JavaConstants.BACKWORD_COMPATIBILITY_OPTION,
467: new String[0]));
468: }
469:
470: if (flattenFiles) {
471: optionMap
472: .put(
473: CommandLineOptionConstants.WSDL2JavaConstants.FLATTEN_FILES_OPTION,
474: new CommandLineOption(
475: CommandLineOptionConstants.WSDL2JavaConstants.FLATTEN_FILES_OPTION,
476: new String[0]));
477: }
478:
479: if (skipMessageReceiver) {
480: optionMap
481: .put(
482: CommandLineOptionConstants.WSDL2JavaConstants.NO_MESSAGE_RECEIVER_OPTION_LONG,
483: new CommandLineOption(
484: CommandLineOptionConstants.WSDL2JavaConstants.NO_MESSAGE_RECEIVER_OPTION_LONG,
485: new String[0]));
486: }
487:
488: if (skipBuildXML) {
489: optionMap
490: .put(
491: CommandLineOptionConstants.WSDL2JavaConstants.NO_BUILD_XML_OPTION_LONG,
492: new CommandLineOption(
493: CommandLineOptionConstants.WSDL2JavaConstants.NO_BUILD_XML_OPTION_LONG,
494: new String[0]));
495: }
496:
497: if (skipWSDL) {
498: optionMap
499: .put(
500: CommandLineOptionConstants.WSDL2JavaConstants.NO_WSDLS_OPTION_LONG,
501: new CommandLineOption(
502: CommandLineOptionConstants.WSDL2JavaConstants.NO_WSDLS_OPTION_LONG,
503: new String[0]));
504: }
505:
506: if (overWrite) {
507: optionMap
508: .put(
509: CommandLineOptionConstants.WSDL2JavaConstants.OVERRIDE_OPTION,
510: new CommandLineOption(
511: CommandLineOptionConstants.WSDL2JavaConstants.OVERRIDE_OPTION,
512: new String[0]));
513: }
514:
515: if (suppressPrefixes) {
516: optionMap
517: .put(
518: CommandLineOptionConstants.WSDL2JavaConstants.SUPPRESS_PREFIXES_OPTION,
519: new CommandLineOption(
520: CommandLineOptionConstants.WSDL2JavaConstants.SUPPRESS_PREFIXES_OPTION,
521: new String[0]));
522: }
523:
524: if (repositoryPath != null) {
525: optionMap
526: .put(
527: CommandLineOptionConstants.WSDL2JavaConstants.REPOSITORY_PATH_OPTION,
528: new CommandLineOption(
529: CommandLineOptionConstants.WSDL2JavaConstants.REPOSITORY_PATH_OPTION,
530: new String[] { repositoryPath }));
531: }
532:
533: if (externalMapping != null) {
534: optionMap
535: .put(
536: CommandLineOptionConstants.WSDL2JavaConstants.EXTERNAL_MAPPING_OPTION,
537: new CommandLineOption(
538: CommandLineOptionConstants.WSDL2JavaConstants.EXTERNAL_MAPPING_OPTION,
539: new String[] { externalMapping }));
540: }
541:
542: if (wsdlVersion != null) {
543: optionMap
544: .put(
545: CommandLineOptionConstants.WSDL2JavaConstants.WSDL_VERSION_OPTION,
546: new CommandLineOption(
547: CommandLineOptionConstants.WSDL2JavaConstants.WSDL_VERSION_OPTION,
548: new String[] { wsdlVersion }));
549: }
550:
551: if (targetSourceFolderLocation != null) {
552: optionMap
553: .put(
554: CommandLineOptionConstants.WSDL2JavaConstants.SOURCE_FOLDER_NAME_OPTION,
555: new CommandLineOption(
556: CommandLineOptionConstants.WSDL2JavaConstants.SOURCE_FOLDER_NAME_OPTION,
557: new String[] { targetSourceFolderLocation }));
558: }
559:
560: if (targetResourcesFolderLocation != null) {
561: optionMap
562: .put(
563: CommandLineOptionConstants.WSDL2JavaConstants.RESOURCE_FOLDER_OPTION,
564: new CommandLineOption(
565: CommandLineOptionConstants.WSDL2JavaConstants.RESOURCE_FOLDER_OPTION,
566: new String[] { targetResourcesFolderLocation }));
567: }
568:
569: Iterator iterator = options.entrySet().iterator();
570: while (iterator.hasNext()) {
571: Map.Entry entry = (Map.Entry) iterator.next();
572: String key = CommandLineOptionConstants.WSDL2JavaConstants.EXTRA_OPTIONTYPE_PREFIX
573: + entry.getKey();
574: String value = (String) entry.getValue();
575: optionMap.put(key, new CommandLineOption(key,
576: new String[] { value }));
577: }
578:
579: optionMap
580: .put(
581: CommandLineOptionConstants.WSDL2JavaConstants.SERVICE_NAME_OPTION,
582: new CommandLineOption(
583: CommandLineOptionConstants.WSDL2JavaConstants.SERVICE_NAME_OPTION,
584: new String[] { serviceName }));
585:
586: optionMap
587: .put(
588: CommandLineOptionConstants.WSDL2JavaConstants.PORT_NAME_OPTION,
589: new CommandLineOption(
590: CommandLineOptionConstants.WSDL2JavaConstants.PORT_NAME_OPTION,
591: new String[] { portName }));
592: // set the namespaces
593: if (!((namespaceToPackages == null) && (namespaceURIs == null))) {
594: optionMap
595: .put(
596: CommandLineOptionConstants.WSDL2JavaConstants.NAME_SPACE_TO_PACKAGE_OPTION,
597: new CommandLineOption(
598: CommandLineOptionConstants.WSDL2JavaConstants.NAME_SPACE_TO_PACKAGE_OPTION,
599: new String[] { getNamespaceToPackagesMap() }));
600: }
601: return optionMap;
602: }
603:
604: private String getNamespaceToPackagesMap()
605: throws MojoFailureException {
606: StringBuffer sb = new StringBuffer();
607: if (namespaceToPackages != null) {
608: sb.append(namespaceToPackages);
609: }
610: if (namespaceURIs != null) {
611: for (int i = 0; i < namespaceURIs.length; i++) {
612: NamespaceURIMapping mapping = namespaceURIs[i];
613: String uri = mapping.getUri();
614: if (uri == null) {
615: throw new MojoFailureException(
616: "A namespace to package mapping requires an uri child element.");
617: }
618: String uriPackageName = mapping.getPackageName();
619: if (uriPackageName == null) {
620: throw new MojoFailureException(
621: "A namespace to package mapping requires a packageName child element.");
622: }
623: if (sb.length() > 0) {
624: sb.append(",");
625: }
626: sb.append(uri);
627: sb.append('=');
628: sb.append(uriPackageName);
629: }
630: }
631: return (sb.length() != 0) ? sb.toString() : null;
632: }
633:
634: /**
635: * Utility method to convert a string into a single item string[]
636: *
637: * @param value
638: * @return Returns String[].
639: */
640: private String[] getStringArray(String value) {
641: String[] values = new String[1];
642: values[0] = value;
643: return values;
644: }
645:
646: public void execute() throws MojoFailureException,
647: MojoExecutionException {
648:
649: fixCompileSourceRoots();
650: fixDependencies();
651: showDependencies();
652:
653: Map commandLineOptions = this .fillOptionMap();
654: CommandLineOptionParser parser = new CommandLineOptionParser(
655: commandLineOptions);
656: try {
657: new CodeGenerationEngine(parser).generate();
658: } catch (CodeGenerationException e) {
659: Throwable t = e;
660: while (t.getCause() != null) {
661: t = t.getCause();
662: }
663: t.printStackTrace();
664: throw new MojoExecutionException(e.getMessage(), e);
665: }
666: }
667:
668: private void showDependencies() {
669: Log log = getLog();
670: if (!log.isDebugEnabled()) {
671: return;
672: }
673: log.debug("The projects dependency artifacts are: ");
674: for (Iterator iter = project.getDependencyArtifacts()
675: .iterator(); iter.hasNext();) {
676: Artifact artifact = (Artifact) iter.next();
677: log.debug(" " + artifact.getGroupId() + ":"
678: + artifact.getArtifactId() + ":"
679: + artifact.getVersion() + ":"
680: + artifact.getClassifier() + ":"
681: + artifact.getScope() + ":" + artifact.getType());
682: }
683: log.debug("The projects transitive artifacts are: ");
684: for (Iterator iter = project.getArtifacts().iterator(); iter
685: .hasNext();) {
686: Artifact artifact = (Artifact) iter.next();
687: log.debug(" " + artifact.getGroupId() + ":"
688: + artifact.getArtifactId() + ":"
689: + artifact.getVersion() + ":"
690: + artifact.getClassifier() + ":"
691: + artifact.getScope() + ":" + artifact.getType());
692: }
693: }
694:
695: private Artifact findArtifact(Collection pCollection,
696: String pGroupId, String pArtifactId) {
697: for (Iterator iter = pCollection.iterator(); iter.hasNext();) {
698: Artifact artifact = (Artifact) iter.next();
699: if (pGroupId.equals(artifact.getGroupId())
700: && pArtifactId.equals(artifact.getArtifactId())) {
701: return artifact;
702: }
703: }
704: return null;
705: }
706:
707: private InheritedArtifact[] getInheritedArtifacts() {
708: final List list = new ArrayList();
709: list.addAll(Arrays.asList(inheritedArtifacts));
710: if ("adb".equals(databindingName)) {
711: list.addAll(Arrays.asList(adbArtifacts));
712: } else if ("xmlbeans".equals(databindingName)) {
713: list.addAll(Arrays.asList(xbeanArtifacts));
714: }
715:
716: return (InheritedArtifact[]) list
717: .toArray(new InheritedArtifact[list.size()]);
718: }
719:
720: private InheritedArtifact getInheritedArtifact(
721: InheritedArtifact[] pInheritedArtifacts, Artifact pArtifact) {
722: for (int i = 0; i < pInheritedArtifacts.length; i++) {
723: InheritedArtifact iArtifact = pInheritedArtifacts[i];
724: if (iArtifact.getGroupId().equals(pArtifact.getGroupId())
725: && iArtifact.getArtifactId().equals(
726: pArtifact.getArtifactId())) {
727: return iArtifact;
728: }
729: }
730: return null;
731: }
732:
733: private void fixDependencies() {
734: final Set set = new HashSet(project.getDependencyArtifacts());
735: final InheritedArtifact[] inhArtifacts = getInheritedArtifacts();
736: for (Iterator iter = pluginArtifacts.iterator(); iter.hasNext();) {
737: final Artifact artifact = (Artifact) iter.next();
738: final InheritedArtifact iArtifact = getInheritedArtifact(
739: inhArtifacts, artifact);
740: if (iArtifact != null) {
741: iArtifact.setAdded();
742: final String groupId = artifact.getGroupId();
743: final String artifactId = artifact.getArtifactId();
744: if (findArtifact(project.getArtifacts(), groupId,
745: artifactId) == null) {
746: getLog().debug(
747: "Adding artifact " + groupId + ":"
748: + artifactId);
749: Artifact artfct = artifactFactory
750: .createArtifactWithClassifier(groupId,
751: artifactId, artifact.getVersion(),
752: artifact.getType(), artifact
753: .getClassifier());
754: artfct.setScope(Artifact.SCOPE_COMPILE);
755: set.add(artfct);
756: } else {
757: getLog()
758: .debug(
759: "The artifact "
760: + artifact.getGroupId()
761: + ":"
762: + artifact.getArtifactId()
763: + " is already present "
764: + " in the project and will not be added.");
765: }
766: }
767: }
768: project.setDependencyArtifacts(set);
769: }
770:
771: private void fixCompileSourceRoots() {
772: File srcDir = new File(outputDirectory, "src");
773: project.addCompileSourceRoot(srcDir.getPath());
774: }
775: }
|