001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.ivy.ant;
019:
020: import java.io.File;
021: import java.net.MalformedURLException;
022: import java.text.ParseException;
023: import java.util.ArrayList;
024: import java.util.Collection;
025: import java.util.HashMap;
026: import java.util.Iterator;
027: import java.util.Map;
028:
029: import org.apache.ivy.core.module.descriptor.ModuleDescriptor;
030: import org.apache.ivy.plugins.parser.m2.PomModuleDescriptorWriter;
031: import org.apache.ivy.plugins.parser.m2.PomModuleDescriptorWriter.ConfigurationScopeMapping;
032: import org.apache.ivy.plugins.parser.xml.XmlModuleDescriptorParser;
033: import org.apache.ivy.util.FileUtil;
034: import org.apache.tools.ant.BuildException;
035: import org.apache.tools.ant.Project;
036:
037: /**
038: * Convert an ivy file to a pom
039: */
040: public class IvyMakePom extends IvyTask {
041: public class Mapping {
042: private String conf;
043: private String scope;
044:
045: public String getConf() {
046: return conf;
047: }
048:
049: public void setConf(String conf) {
050: this .conf = conf;
051: }
052:
053: public String getScope() {
054: return scope;
055: }
056:
057: public void setScope(String scope) {
058: this .scope = scope;
059: }
060: }
061:
062: private File pomFile = null;
063:
064: private File headerFile = null;
065:
066: private File ivyFile = null;
067:
068: private Collection mappings = new ArrayList();
069:
070: public File getPomFile() {
071: return pomFile;
072: }
073:
074: public void setPomFile(File file) {
075: pomFile = file;
076: }
077:
078: public File getIvyFile() {
079: return ivyFile;
080: }
081:
082: public void setIvyFile(File ivyFile) {
083: this .ivyFile = ivyFile;
084: }
085:
086: public File getHeaderFile() {
087: return headerFile;
088: }
089:
090: public void setHeaderFile(File headerFile) {
091: this .headerFile = headerFile;
092: }
093:
094: public Mapping createMapping() {
095: Mapping mapping = new Mapping();
096: this .mappings.add(mapping);
097: return mapping;
098: }
099:
100: public void doExecute() throws BuildException {
101: try {
102: if (ivyFile == null) {
103: throw new BuildException(
104: "source ivy file is required for makepom task");
105: }
106: if (pomFile == null) {
107: throw new BuildException(
108: "destination pom file is required for makepom task");
109: }
110: ModuleDescriptor md = XmlModuleDescriptorParser
111: .getInstance().parseDescriptor(getSettings(),
112: ivyFile.toURL(), false);
113: PomModuleDescriptorWriter
114: .write(
115: md,
116: headerFile == null ? null : FileUtil
117: .readEntirely(getHeaderFile()),
118: mappings.isEmpty() ? PomModuleDescriptorWriter.DEFAULT_MAPPING
119: : new ConfigurationScopeMapping(
120: getMappingsMap()), pomFile);
121: } catch (MalformedURLException e) {
122: throw new BuildException(
123: "unable to convert given ivy file to url: "
124: + ivyFile + ": " + e, e);
125: } catch (ParseException e) {
126: log(e.getMessage(), Project.MSG_ERR);
127: throw new BuildException("syntax errors in ivy file "
128: + ivyFile + ": " + e, e);
129: } catch (Exception e) {
130: throw new BuildException(
131: "impossible convert given ivy file to pom file: "
132: + e + " from=" + ivyFile + " to=" + pomFile,
133: e);
134: }
135: }
136:
137: private Map getMappingsMap() {
138: Map mappingsMap = new HashMap();
139: for (Iterator iter = mappings.iterator(); iter.hasNext();) {
140: Mapping mapping = (Mapping) iter.next();
141: mappingsMap.put(mapping.getConf(), mapping.getScope());
142: }
143: return mappingsMap;
144: }
145: }
|