1 package org.apache.maven.index.artifact;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import javax.inject.Named;
23 import javax.inject.Singleton;
24 import java.io.File;
25 import java.io.FileInputStream;
26 import java.io.IOException;
27 import java.util.HashMap;
28 import java.util.Map;
29 import java.util.Properties;
30
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34
35
36
37
38
39
40
41 @Singleton
42 @Named
43 public class DefaultArtifactPackagingMapper
44 implements ArtifactPackagingMapper
45 {
46
47 private final Logger logger = LoggerFactory.getLogger( getClass() );
48
49 protected Logger getLogger()
50 {
51 return logger;
52 }
53
54 public static final String MAPPING_PROPERTIES_FILE = "packaging2extension-mapping.properties";
55
56 private File propertiesFile;
57
58 private volatile Map<String, String> packaging2extensionMapping;
59
60 private static final Map<String, String> DEFAULTS;
61
62 static
63 {
64 DEFAULTS = new HashMap<>();
65 DEFAULTS.put( "ejb-client", "jar" );
66 DEFAULTS.put( "ejb", "jar" );
67 DEFAULTS.put( "rar", "jar" );
68 DEFAULTS.put( "par", "jar" );
69 DEFAULTS.put( "maven-plugin", "jar" );
70 DEFAULTS.put( "maven-archetype", "jar" );
71 DEFAULTS.put( "plexus-application", "jar" );
72 DEFAULTS.put( "eclipse-plugin", "jar" );
73 DEFAULTS.put( "eclipse-feature", "jar" );
74 DEFAULTS.put( "eclipse-application", "zip" );
75 DEFAULTS.put( "nexus-plugin", "jar" );
76 DEFAULTS.put( "java-source", "jar" );
77 DEFAULTS.put( "javadoc", "jar" );
78 DEFAULTS.put( "test-jar", "jar" );
79 }
80
81 public void setPropertiesFile( File propertiesFile )
82 {
83 this.propertiesFile = propertiesFile;
84 this.packaging2extensionMapping = null;
85 }
86
87 public Map<String, String> getPackaging2extensionMapping()
88 {
89 if ( packaging2extensionMapping == null )
90 {
91 synchronized ( this )
92 {
93 if ( packaging2extensionMapping == null )
94 {
95 packaging2extensionMapping = new HashMap<>();
96
97
98 packaging2extensionMapping.putAll( DEFAULTS );
99
100 if ( propertiesFile != null && propertiesFile.exists() )
101 {
102 getLogger().info( "Found user artifact packaging mapping file, applying it..." );
103
104 Properties userMappings = new Properties();
105
106 try ( FileInputStream fis = new FileInputStream( propertiesFile ) )
107 {
108 userMappings.load( fis );
109
110 if ( userMappings.keySet().size() > 0 )
111 {
112 for ( Object key : userMappings.keySet() )
113 {
114 packaging2extensionMapping.put( key.toString(),
115 userMappings.getProperty( key.toString() ) );
116 }
117
118 getLogger().info(
119 propertiesFile.getAbsolutePath()
120 + " user artifact packaging mapping file contained "
121 + userMappings.keySet().size() + " mappings, applied them all succesfully." );
122 }
123 }
124 catch ( IOException e )
125 {
126 getLogger().warn(
127 "Got IO exception during read of file: " + propertiesFile.getAbsolutePath() );
128 }
129
130 }
131 else
132 {
133
134 getLogger().debug(
135 "User artifact packaging mappings file not found, will work with defaults..." );
136 }
137 }
138 }
139 }
140
141 return packaging2extensionMapping;
142 }
143
144 public void setPackaging2extensionMapping( Map<String, String> packaging2extensionMapping )
145 {
146 this.packaging2extensionMapping = packaging2extensionMapping;
147 }
148
149 public Map<String, String> getDefaults()
150 {
151 return DEFAULTS;
152 }
153
154 public String getExtensionForPackaging( String packaging )
155 {
156 if ( packaging == null )
157 {
158 return "jar";
159 }
160
161
162 return getPackaging2extensionMapping().getOrDefault( packaging, packaging );
163 }
164 }