blob: f31855dde79cd3dc224b2c557049ba804a3af755 [file] [log] [blame]
Stefano Lenzi476013d2007-09-21 23:59:54 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
Stuart McCullochbdb81682007-12-16 09:46:19 +000019package org.apache.felix.obr.plugin;
Stefano Lenzi476013d2007-09-21 23:59:54 +000020
Stuart McCulloch7a49c282008-01-25 08:08:15 +000021
Stefano Lenzi476013d2007-09-21 23:59:54 +000022import java.io.File;
Stuart McCulloch473e9942008-01-28 05:23:50 +000023import java.net.URI;
Stefano Lenzi476013d2007-09-21 23:59:54 +000024
25import org.apache.maven.artifact.repository.ArtifactRepository;
Stefano Lenzi476013d2007-09-21 23:59:54 +000026import org.apache.maven.plugin.AbstractMojo;
27import org.apache.maven.plugin.MojoExecutionException;
28import org.apache.maven.project.MavenProject;
29import org.apache.maven.settings.Settings;
30
Stuart McCulloch7a49c282008-01-25 08:08:15 +000031
Stefano Lenzi476013d2007-09-21 23:59:54 +000032/**
33 * construct the repository.xml with a project Maven compiled
34 *
35 * @goal repository
36 * @phase install
37 * @requiresDependencyResolution compile
38 * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
39 */
Stuart McCulloch7a49c282008-01-25 08:08:15 +000040public class ObrInstall extends AbstractMojo
41{
Stefano Lenzi476013d2007-09-21 23:59:54 +000042 /**
43 * The local Maven repository.
44 *
45 * @parameter expression="${localRepository}"
46 * @required
47 */
48 private ArtifactRepository m_localRepo;
49
50 /**
51 * path to the repository.xml.
52 *
53 * @parameter expression="${repository-path}"
54 * @require
55 */
56 private String m_repositoryPath;
57
58 /**
59 * Project in use.
60 *
61 * @parameter expression="${project}"
62 * @require
63 */
64
65 private MavenProject m_project;
66
67 /**
68 * setting of maven.
69 *
70 * @parameter expression="${settings}"
71 * @require
72 */
73
74 private Settings m_settings;
Stuart McCulloch7a49c282008-01-25 08:08:15 +000075
Stefano Lenzi476013d2007-09-21 23:59:54 +000076 /**
77 * Enable/Disable this goal
78 * @description If true evrything the goal do nothing, the goal just skip over
79 * @parameter expression="${maven.obr.installToLocalOBR}" default-value="true"
80 */
Stuart McCulloch7a49c282008-01-25 08:08:15 +000081 private boolean installToLocalOBR;
Stefano Lenzi476013d2007-09-21 23:59:54 +000082
83 /**
84 * path to file in the maven local repository.
85 */
86 private String m_fileInLocalRepo;
87
Stuart McCulloch7a49c282008-01-25 08:08:15 +000088
Stefano Lenzi476013d2007-09-21 23:59:54 +000089 /**
90 * main method for this goal.
91 * @implements org.apache.maven.plugin.Mojo.execute
92 * @throws MojoExecutionException if the plugin failed
93 */
Stuart McCulloch7a49c282008-01-25 08:08:15 +000094 public void execute() throws MojoExecutionException
95 {
96 getLog().info( "Obr Plugin starts:" );
97 if ( !installToLocalOBR )
Stefano Lenzi476013d2007-09-21 23:59:54 +000098 {
Stuart McCulloch7a49c282008-01-25 08:08:15 +000099 getLog().info( "maven-obr-plugin:repository goal is disable due to one of the following reason:" );
100 getLog().info( " - 'installToLocalOBR' configuration set to false" );
101 getLog().info( " - JVM property maven.obr.installToLocalOBR set to false" );
102 return;
Stefano Lenzi476013d2007-09-21 23:59:54 +0000103 }
104
Stuart McCulloch7a49c282008-01-25 08:08:15 +0000105 if ( m_repositoryPath == null )
106 {
107 m_repositoryPath = "file:/" + m_localRepo.getBasedir() + File.separator + "repository.xml";
Stuart McCulloch473e9942008-01-28 05:23:50 +0000108 getLog().info( "-Drepository-path is not set, using default repository: " + m_repositoryPath );
Stuart McCulloch7a49c282008-01-25 08:08:15 +0000109 }
110
111 PathFile file = new PathFile( m_repositoryPath );
112 if ( file.isExists() )
113 {
114 if ( !m_repositoryPath.startsWith( "file:/" ) )
115 {
116 m_repositoryPath = "file:/" + m_repositoryPath;
117 }
Stefano Lenzi476013d2007-09-21 23:59:54 +0000118 }
119
120 // locate the obr.xml file
Stuart McCulloch473e9942008-01-28 05:23:50 +0000121 URI obrXml = ObrUtils.findObrXml( m_project.getResources() );
122
Stefano Lenzi476013d2007-09-21 23:59:54 +0000123 // the obr.xml file is not present
Stuart McCulloch473e9942008-01-28 05:23:50 +0000124 if ( null == obrXml )
Stuart McCulloch7a49c282008-01-25 08:08:15 +0000125 {
Stuart McCulloch473e9942008-01-28 05:23:50 +0000126 getLog().info( "obr.xml is not present, use default" );
Stefano Lenzi476013d2007-09-21 23:59:54 +0000127 }
128
129 // get the path to local maven repository
Stuart McCulloch7a49c282008-01-25 08:08:15 +0000130 file = new PathFile( PathFile.uniformSeparator( m_settings.getLocalRepository() ) + File.separator
131 + PathFile.uniformSeparator( m_localRepo.pathOf( m_project.getArtifact() ) ) );
132
133 if ( file.isExists() )
134 {
135 m_fileInLocalRepo = file.getOnlyAbsoluteFilename();
136 }
137 else
138 {
139 getLog().error(
140 "file not found in local repository: " + m_settings.getLocalRepository() + File.separator
141 + m_localRepo.pathOf( m_project.getArtifact() ) );
142 getLog().error(
143 "file not found in local repository: " + m_localRepo.getBasedir() + File.separator
144 + m_localRepo.pathOf( m_project.getArtifact() ) );
Stefano Lenzi476013d2007-09-21 23:59:54 +0000145 return;
146 }
147
148 // verify the repository.xml
Stuart McCulloch7a49c282008-01-25 08:08:15 +0000149 PathFile fileRepo = new PathFile( m_repositoryPath );
150 if ( fileRepo.isRelative() )
151 {
152 fileRepo.setBaseDir( m_settings.getLocalRepository() );
153 }
Stefano Lenzi476013d2007-09-21 23:59:54 +0000154
155 // create the folder to the repository
Stuart McCulloch7a49c282008-01-25 08:08:15 +0000156 PathFile repoExist = new PathFile( fileRepo.getAbsolutePath() );
157 if ( !repoExist.isExists() )
158 {
159 fileRepo.createPath();
160 }
Stefano Lenzi476013d2007-09-21 23:59:54 +0000161
162 // build the user configuration (use default)
163 Config user = new Config();
164
Stuart McCulloch7a49c282008-01-25 08:08:15 +0000165 getLog().debug( "Maven2 Local File repository = " + fileRepo.getAbsoluteFilename() );
Stuart McCulloch473e9942008-01-28 05:23:50 +0000166 getLog().debug( "OBR repository = " + obrXml );
Stuart McCulloch7a49c282008-01-25 08:08:15 +0000167
Stuart McCulloch473e9942008-01-28 05:23:50 +0000168 ObrUpdate obrUpdate = new ObrUpdate( fileRepo, obrXml, m_project, m_fileInLocalRepo, PathFile
Stuart McCulloch7a49c282008-01-25 08:08:15 +0000169 .uniformSeparator( m_settings.getLocalRepository() ), user, getLog() );
170 try
171 {
Stefano Lenzi476013d2007-09-21 23:59:54 +0000172 obrUpdate.updateRepository();
Stuart McCulloch7a49c282008-01-25 08:08:15 +0000173 }
174 catch ( MojoExecutionException e )
175 {
Stefano Lenzi476013d2007-09-21 23:59:54 +0000176 e.printStackTrace();
Stuart McCulloch7a49c282008-01-25 08:08:15 +0000177 throw new MojoExecutionException( "MojoFailureException" );
Stefano Lenzi476013d2007-09-21 23:59:54 +0000178 }
179 }
180
181}