Save This Page
Home » maven-ant-tasks-2.1.0-src » org.apache » maven » artifact » ant » [javadoc | source]
    1   package org.apache.maven.artifact.ant;
    2   
    3   /*
    4    * Licensed to the Apache Software Foundation (ASF) under one
    5    * or more contributor license agreements.  See the NOTICE file
    6    * distributed with this work for additional information
    7    * regarding copyright ownership.  The ASF licenses this file
    8    * to you under the Apache License, Version 2.0 (the
    9    * "License"); you may not use this file except in compliance
   10    * with the License.  You may obtain a copy of the License at
   11    *
   12    *  http://www.apache.org/licenses/LICENSE-2.0
   13    *
   14    * Unless required by applicable law or agreed to in writing,
   15    * software distributed under the License is distributed on an
   16    * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   17    * KIND, either express or implied.  See the License for the
   18    * specific language governing permissions and limitations
   19    * under the License.
   20    */
   21   
   22   import org.apache.maven.artifact.Artifact;
   23   import org.apache.maven.artifact.repository.ArtifactRepository;
   24   import org.apache.maven.model.Build;
   25   import org.apache.maven.model.CiManagement;
   26   import org.apache.maven.model.Contributor;
   27   import org.apache.maven.model.Dependency;
   28   import org.apache.maven.model.DependencyManagement;
   29   import org.apache.maven.model.Developer;
   30   import org.apache.maven.model.DistributionManagement;
   31   import org.apache.maven.model.IssueManagement;
   32   import org.apache.maven.model.License;
   33   import org.apache.maven.model.MailingList;
   34   import org.apache.maven.model.Model;
   35   import org.apache.maven.model.Organization;
   36   import org.apache.maven.model.Reporting;
   37   import org.apache.maven.model.Repository;
   38   import org.apache.maven.model.Scm;
   39   import org.apache.maven.profiles.ProfileManager;
   40   import org.apache.maven.project.DefaultProjectBuilderConfiguration;
   41   import org.apache.maven.project.MavenProject;
   42   import org.apache.maven.project.MavenProjectBuilder;
   43   import org.apache.maven.project.MavenProjectHelper;
   44   import org.apache.maven.project.ProjectBuilderConfiguration;
   45   import org.apache.maven.project.ProjectBuildingException;
   46   import org.apache.tools.ant.BuildException;
   47   import org.apache.tools.ant.Project;
   48   import org.apache.tools.ant.PropertyHelper;
   49   import org.codehaus.plexus.util.introspection.ReflectionValueExtractor;
   50   
   51   import java.io.File;
   52   import java.util.ArrayList;
   53   import java.util.Hashtable;
   54   import java.util.Iterator;
   55   import java.util.List;
   56   import java.util.Properties;
   57   
   58   /**
   59    * A POM typedef. Also an Ant Task that registers a handler called POMPropertyHelper that intercepts all calls to
   60    * property value resolution and replies instead of Ant to properties that start with the id of the pom. Example:
   61    * ${maven.project.artifactId}
   62    * 
   63    * @author <a href="mailto:brett@apache.org">Brett Porter</a>
   64    * @author <a href="mailto:nicolaken@apache.org">Nicola Ken Barozzi</a>
   65    * @version $Id: Pom.java 884230 2009-11-25 19:15:17Z pgier $
   66    */
   67   public class Pom
   68       extends AbstractArtifactWithRepositoryTask
   69   {
   70       /**
   71        * The id refering to an existing pom object in the current Ant project.
   72        */
   73       private String refid;
   74   
   75       /**
   76        * The id of this pom object to be stored in the current Ant project.
   77        */
   78       private String antId;
   79   
   80       /**
   81        * The maven project represented by this pom
   82        */
   83       private MavenProject mavenProject;
   84   
   85       /**
   86        * The file from which the pom was loaded.
   87        */
   88       private File file;
   89   
   90       /**
   91        * The list of profiles to either activate or deactivate for this pom.
   92        */
   93       private List profiles = new ArrayList();
   94   
   95       private boolean inheritAllProperties = true;
   96   
   97       /**
   98        * The property intercepter.
   99        */
  100       private final POMPropertyHelper helper = new POMPropertyHelper();
  101   
  102       public String getRefid()
  103       {
  104           return refid;
  105       }
  106   
  107       /**
  108        * The ID used to retrieve this pom object from the Ant project.
  109        * 
  110        * @param refid
  111        */
  112       public void setRefid( String refid )
  113       {
  114           this.refid = refid;
  115       }
  116   
  117       /**
  118        * The ID used to store this pom object in the Ant project.
  119        * 
  120        * @param id
  121        */
  122       public void setId( String id )
  123       {
  124           this.antId = id;
  125       }
  126   
  127       /**
  128        * Retrieve the pom object from the current Ant project using the configured refid.
  129        * 
  130        * @param refid
  131        * @return
  132        */
  133       protected void getPomFromAntProject( String refid )
  134       {
  135           if ( refid == null )
  136           {
  137               throw new BuildException( "POM refid is null." );
  138           }
  139   
  140           if ( getProject().getReference( refid ) == null )
  141           {
  142               throw new BuildException( "Unable to locate POM reference: '" + refid + "'" );
  143           }
  144   
  145           Pom thePom = (Pom) getProject().getReference( refid );
  146           mavenProject = thePom.getMavenProject();
  147           file = thePom.getFile();
  148       }
  149   
  150       public void setMavenProject( MavenProject mavenProject )
  151       {
  152           this.mavenProject = mavenProject;
  153       }
  154   
  155       public File getFile()
  156       {
  157           return file;
  158       }
  159   
  160       public void setFile( File file )
  161       {
  162           this.file = file;
  163       }
  164   
  165       public List getProfiles()
  166       {
  167           return profiles;
  168       }
  169   
  170       public void addProfile( Profile activeProfile )
  171       {
  172           this.profiles.add( activeProfile );
  173       }
  174   
  175       public Artifact getArtifact()
  176       {
  177           return getMavenProject().getArtifact();
  178       }
  179   
  180       public void attach( AttachedArtifact attached )
  181       {
  182           MavenProjectHelper helper = (MavenProjectHelper) lookup( MavenProjectHelper.ROLE );
  183           MavenProject project = getMavenProject();
  184           if ( attached.getClassifier() != null )
  185           {
  186               helper.attachArtifact( project, attached.getType(), attached.getClassifier(), attached.getFile() );
  187           }
  188           else
  189           {
  190               helper.attachArtifact( project, attached.getType(), attached.getFile() );
  191           }
  192       }
  193   
  194       public List getAttachedArtifacts()
  195       {
  196           return getMavenProject().getAttachedArtifacts();
  197       }
  198   
  199       public void initialiseMavenProject( MavenProjectBuilder builder, ArtifactRepository localRepository )
  200       {
  201           if ( file != null )
  202           {
  203               addAntRepositoriesToProfileManager();
  204               ProjectBuilderConfiguration builderConfig = this.createProjectBuilderConfig( localRepository );
  205               try
  206               {
  207                   mavenProject = builder.build( file, builderConfig );
  208               }
  209               catch ( ProjectBuildingException e )
  210               {
  211                   throw new BuildException( "Unable to initialize POM " + file.getName() + ": " + e.getMessage(), e );
  212               }
  213           }
  214           else if ( refid != null )
  215           {
  216               this.getPomFromAntProject( refid );
  217           }
  218       }
  219   
  220       protected MavenProject getMavenProject()
  221       {
  222           if ( mavenProject == null )
  223           {
  224               mavenProject = createMinimalProject( createLocalArtifactRepository() );
  225           }
  226           return mavenProject;
  227       }
  228   
  229       public String getArtifactId()
  230       {
  231           return getMavenProject().getArtifactId();
  232       }
  233   
  234       public Build getBuild()
  235       {
  236           return getMavenProject().getBuild();
  237       }
  238   
  239       public CiManagement getCiManagement()
  240       {
  241           return getMavenProject().getCiManagement();
  242       }
  243   
  244       public List getContributors()
  245       {
  246           return getMavenProject().getContributors();
  247       }
  248   
  249       public List getDependencies()
  250       {
  251           return getMavenProject().getDependencies();
  252       }
  253   
  254       public DependencyManagement getDependencyManagement()
  255       {
  256           return getMavenProject().getDependencyManagement();
  257       }
  258   
  259       public String getDescription()
  260       {
  261           return getMavenProject().getDescription();
  262       }
  263   
  264       public List getDevelopers()
  265       {
  266           return getMavenProject().getDevelopers();
  267       }
  268   
  269       public DistributionManagement getDistributionManagement()
  270       {
  271           return getMavenProject().getDistributionManagement();
  272       }
  273   
  274       public String getGroupId()
  275       {
  276           return getMavenProject().getGroupId();
  277       }
  278   
  279       public String getInceptionYear()
  280       {
  281           return getMavenProject().getInceptionYear();
  282       }
  283   
  284       public IssueManagement getIssueManagement()
  285       {
  286           return getMavenProject().getIssueManagement();
  287       }
  288   
  289       public List getLicenses()
  290       {
  291           return getMavenProject().getLicenses();
  292       }
  293   
  294       public List getMailingLists()
  295       {
  296           return getMavenProject().getMailingLists();
  297       }
  298   
  299       public String getModelVersion()
  300       {
  301           return getMavenProject().getModelVersion();
  302       }
  303   
  304       public List getModules()
  305       {
  306           return getMavenProject().getModules();
  307       }
  308   
  309       public String getName()
  310       {
  311           return getMavenProject().getName();
  312       }
  313   
  314       public Organization getOrganization()
  315       {
  316           return getMavenProject().getOrganization();
  317       }
  318   
  319       public String getPackaging()
  320       {
  321           return getMavenProject().getPackaging();
  322       }
  323   
  324       public List getPluginRepositories()
  325       {
  326           return getMavenProject().getPluginRepositories();
  327       }
  328   
  329       public Reporting getReporting()
  330       {
  331           return getMavenProject().getReporting();
  332       }
  333   
  334       public List getRepositories()
  335       {
  336           return getMavenProject().getRepositories();
  337       }
  338   
  339       public Scm getScm()
  340       {
  341           return getMavenProject().getScm();
  342       }
  343   
  344       public String getUrl()
  345       {
  346           return getMavenProject().getUrl();
  347       }
  348   
  349       public String getVersion()
  350       {
  351           return getMavenProject().getVersion();
  352       }
  353   
  354       public String getId()
  355       {
  356           return getMavenProject().getId();
  357       }
  358   
  359       /**
  360        * Registers POMPropertyHelper as a property interceptor
  361        */
  362       protected void doExecute()
  363       {
  364           if ( getId() == null )
  365           {
  366               throw new BuildException( "id required for pom task" );
  367           }
  368           ArtifactRepository localRepo = createLocalArtifactRepository();
  369           MavenProjectBuilder projectBuilder = (MavenProjectBuilder) lookup( MavenProjectBuilder.ROLE );
  370           initialiseMavenProject( projectBuilder, localRepo );
  371   
  372           Project antProject = getProject();
  373   
  374           // Add a reference to this task/type
  375           antProject.addReference( antId, this );
  376   
  377           // Register the property intercepter
  378           PropertyHelper phelper = PropertyHelper.getPropertyHelper( antProject );
  379           helper.setNext( phelper.getNext() );
  380           helper.setProject( antProject );
  381           phelper.setNext( helper );
  382       }
  383   
  384       /**
  385        * The property intercepter that handles the calls for "pom." properties
  386        */
  387       private class POMPropertyHelper
  388           extends PropertyHelper
  389       {
  390           /**
  391            * The method that gets called by Ant with every request of property
  392            */
  393           public Object getPropertyHook( String ns, String name, boolean user )
  394           {
  395               String prefix = antId + ".";
  396   
  397               if ( !name.startsWith( prefix ) )
  398               {
  399                   // pass on to next interceptor
  400                   return super.getPropertyHook( ns, name, user );
  401               }
  402               try
  403               {
  404                   // else handle the property resolution
  405                   String expression = name.substring( prefix.length() );
  406                   return getPOMValue( "project." + expression );
  407               }
  408               catch ( Exception ex )
  409               {
  410                   ex.printStackTrace();
  411                   return null;
  412               }
  413           }
  414   
  415           private static final String PROPERTIES_PREFIX = "project.properties.";
  416   
  417           private Object getPOMValue( String expression )
  418           {
  419               Object value = null;
  420   
  421               try
  422               {
  423                   if ( expression.startsWith( PROPERTIES_PREFIX ) )
  424                   {
  425                       expression = expression.substring( PROPERTIES_PREFIX.length() );
  426                       value = getMavenProject().getProperties().get( expression );
  427                   }
  428                   else
  429                   {
  430                       value = ReflectionValueExtractor.evaluate( expression, getMavenProject() );
  431                   }
  432               }
  433               catch ( Exception e )
  434               {
  435                   throw new BuildException( "Error extracting expression from POM", e );
  436               }
  437   
  438               return value;
  439           }
  440   
  441       }
  442   
  443       /**
  444        * The repositories defined in the ant "pom" task need to be added manually to the profile manager. Otherwise they
  445        * won't be available when resolving the parent pom. MANTTASKS-87
  446        */
  447       private void addAntRepositoriesToProfileManager()
  448       {
  449           List remoteRepositories = this.getRemoteRepositories();
  450   
  451           if ( remoteRepositories == null || remoteRepositories.isEmpty() )
  452           {
  453               return;
  454           }
  455           org.apache.maven.model.Profile repositoriesProfile = new org.apache.maven.model.Profile();
  456           repositoriesProfile.setId( "maven-ant-tasks-repo-profile" );
  457   
  458           Iterator iter = remoteRepositories.iterator();
  459           while ( iter.hasNext() )
  460           {
  461               RemoteRepository antRepo = (RemoteRepository) iter.next();
  462               Repository mavenRepo = new Repository();
  463               mavenRepo.setId( antRepo.getId() );
  464               mavenRepo.setUrl( antRepo.getUrl() );
  465               repositoriesProfile.addRepository( mavenRepo );
  466           }
  467   
  468           getProfileManager().addProfile( repositoriesProfile );
  469           getProfileManager().explicitlyActivate( repositoriesProfile.getId() );
  470       }
  471   
  472       private ProfileManager getActivatedProfiles()
  473       {
  474           ProfileManager profileManager = getProfileManager();
  475   
  476           Iterator it = getProfiles().iterator();
  477           while ( it.hasNext() )
  478           {
  479               Profile profile = (Profile) it.next();
  480   
  481               if ( profile.getId() == null )
  482               {
  483                   throw new BuildException( "Attribute \"id\" is required for profile in pom type." );
  484               }
  485   
  486               if ( profile.getActive() == null || Boolean.valueOf( profile.getActive() ).booleanValue() )
  487               {
  488                   profileManager.explicitlyActivate( profile.getId() );
  489               }
  490               else
  491               {
  492                   profileManager.explicitlyDeactivate( profile.getId() );
  493               }
  494   
  495           }
  496           return profileManager;
  497       }
  498   
  499       /**
  500        * Create a project builder configuration to be used when initializing the maven project.
  501        * 
  502        * @return
  503        */
  504       private ProjectBuilderConfiguration createProjectBuilderConfig( ArtifactRepository localArtifactRepository )
  505       {
  506           ProjectBuilderConfiguration builderConfig = new DefaultProjectBuilderConfiguration();
  507           builderConfig.setLocalRepository( localArtifactRepository );
  508           builderConfig.setGlobalProfileManager( this.getActivatedProfiles() );
  509           builderConfig.setUserProperties( getAntProjectProperties() );
  510           builderConfig.setExecutionProperties( getAntProjectProperties() );
  511   
  512           return builderConfig;
  513       }
  514   
  515       /**
  516        * Convert the Hashtable of Ant project properties to a Properties object
  517        * 
  518        * @return The Ant project properties
  519        */
  520       public Properties getAntProjectProperties()
  521       {
  522           Properties properties = new Properties();
  523           Hashtable propsTable = null;
  524           if ( this.isInheritAllProperties() )
  525           {
  526               propsTable = getProject().getProperties();
  527           }
  528           else
  529           {
  530               propsTable = getProject().getUserProperties();
  531           }
  532           Iterator propsIter = propsTable.keySet().iterator();
  533   
  534           while ( propsIter.hasNext() )
  535           {
  536               String key = (String) propsIter.next();
  537               String value = (String) propsTable.get( key );
  538               properties.setProperty( key, value );
  539           }
  540   
  541           return properties;
  542       }
  543   
  544       /**
  545        * If set to true, all properties are passed to the maven pom. If set to false, only user properties are passed to
  546        * the pom.
  547        * 
  548        * @param inheritAllProperties
  549        */
  550       public void setInheritAllProperties( boolean inheritAllProperties )
  551       {
  552           this.inheritAllProperties = inheritAllProperties;
  553       }
  554   
  555       public boolean isInheritAllProperties()
  556       {
  557           return inheritAllProperties;
  558       }
  559   
  560       public Model getModel()
  561       {
  562           return getMavenProject().getModel();
  563       }
  564   
  565       public void setGroupId( String groupId )
  566       {
  567           getMavenProject().setGroupId( groupId );
  568       }
  569   
  570       public void setArtifactId( String artifactId )
  571       {
  572           getMavenProject().setArtifactId( artifactId );
  573       }
  574   
  575       public void setVersion( String version )
  576       {
  577           getMavenProject().setVersion( version );
  578       }
  579   
  580       public void addConfiguredCiManagement( CiManagement ciManagement )
  581       {
  582           getMavenProject().setCiManagement( ciManagement );
  583       }
  584   
  585       public void addConfiguredContributor ( Contributor contributor )
  586       {
  587           getMavenProject().addContributor( contributor );
  588       }
  589   
  590       public void addConfiguredDependency( Dependency dependency )
  591       {
  592           getMavenProject().getDependencies().add( dependency );
  593       }
  594   
  595       public void addConfiguredDependencyManagement( DependencyManagement dependencyManagement )
  596       {
  597           getMavenProject().getDependencyManagement().setDependencies( dependencyManagement.getDependencies() );
  598       }
  599   
  600       public void setDescription( String description )
  601       {
  602           getMavenProject().setDescription( description );
  603       }
  604   
  605       public void addConfiguredDeveloper( Developer developer )
  606       {
  607           getMavenProject().addDeveloper( developer );
  608       }
  609   
  610       public void setInceptionYear( String inceptionYear )
  611       {
  612           getMavenProject().setInceptionYear( inceptionYear );
  613       }
  614   
  615       public void addConfiguredIssueManagement( IssueManagement issueManagement )
  616       {
  617           getMavenProject().setIssueManagement( issueManagement );
  618       }
  619       
  620       public void addConfiguredLicense ( License license )
  621       {
  622           getMavenProject().addLicense( license );
  623       }
  624       
  625       public void addConfiguredMailingLists( MailingList mailingList )
  626       {
  627           getMavenProject().addMailingList( mailingList );
  628       }
  629   
  630       public void setName( String name )
  631       {
  632           getMavenProject().setName( name );
  633       }
  634   
  635       public void addConfiguredOrganization( Organization organization )
  636       {
  637           getMavenProject().setOrganization( organization );
  638       }
  639   
  640       public void setPackaging( String packaging )
  641       {
  642           getMavenProject().setPackaging( packaging );
  643       }
  644   
  645       public void addConfiguredScm( Scm scm )
  646       {
  647           getMavenProject().setScm( scm );
  648       }
  649   
  650       public void setUrl( String url )
  651       {
  652           getMavenProject().setUrl( url );
  653       }
  654   
  655   }

Save This Page
Home » maven-ant-tasks-2.1.0-src » org.apache » maven » artifact » ant » [javadoc | source]