Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as Java by qqq ( 15 years ago )
/**
 * The plug-in <i>maven-plugin-tibco</i> is aimed for building TIBCO projects including TIBCO BusinessWorks projects under Apache Maven tool. 
 * It also provide the ability to manage project dependencies by standard Maven facilities.
 *
 * For the sake of usability there is <i>maven-archetype-tibco</i> archetype is created to simplify TIBCO projects creation.
 */
package com.db.gmld.maven.tibco.bw.ext;


import static com.db.gmld.maven.tibco.bw.BaseMojo.LOG_INFO_LINE;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.Properties;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.project.MavenProject;

import com.db.gmld.maven.tibco.bw.BaseMojo.PACKAGING;
import com.db.gmld.util.FSUtils;


/**
 * Collects third-party dependencies of current project in TIBCO BW modules.
 * 
 * @author grumbler
 * 
 * @goal collect-dependencies-bw
 * @requiresProject true
 * @requiresDirectInvocation true
 * @requiresDependencyResolution runtime
 */
public class CollectThirdPartyDependenciesMojo extends AbstractMojo {
 
 /**
  * This Maven project.
  * @parameter expression="${project}"
  * @required
  * @readonly
  */
 private MavenProject project;
 /**
     * The Maven Session Object
     * @parameter expression="${session}"
     * @required
     */
    private MavenSession session;
    
    /** Maven session property for indicating the execution from main (parent) POM. */
    private static final String PARENT_POM_EXECUTED = "PARENT_POM_EXECUTED";
    

 /**
  * @see org.apache.maven.plugin.Mojo#execute()
  */
 public void execute() throws MojoExecutionException, MojoFailureException {
  if (session.getUserProperties() != null && session.getUserProperties().getProperty(PARENT_POM_EXECUTED) != null) {
   if (Boolean.parseBoolean(session.getUserProperties().getProperty(PARENT_POM_EXECUTED))) {
    if (project.getPackaging().equals(PACKAGING.EAR.getPackaging())) {
     getLog().info(LOG_INFO_LINE);
     getLog().info("Processing TIBCO EAR project " + project.getName() + " ...");
     collectDependencies();
    } else if (project.getPackaging().equals(PACKAGING.PROJLIB.getPackaging())) {
     getLog().info(LOG_INFO_LINE);
     getLog().info("Processing TIBCO PROJLIB project " + project.getName() + " ...");
     collectDependencies();
    } else {
     getLog().info("Skipping non TIBCO BusinessWorks project.");
    }
   }
  } else {
   if (project.hasParent()) {
    getLog().error("This is not main project!");
    getLog().error("Please, execute this goal on the main (parent) project!");
    throw new MojoExecutionException("The goal "Collect third-party dependencies of TIBCO projects" needs execution on the main (parent) project!");
   } else {
    if (session.getUserProperties() == null)
     session.setUserProperties(new Properties());
    session.getUserProperties().setProperty(PARENT_POM_EXECUTED, Boolean.TRUE.toString());
   }
  }
 }
 
 /**
  * Collect current project's 3d party libs.
  * @throws MojoExecutionException
  * @throws MojoFailureException
  */
 private void collectDependencies() throws MojoExecutionException, MojoFailureException {
  getLog().info("Collect third-party dependencies ...");
  getLog().info(LOG_INFO_LINE);
  @SuppressWarnings("unchecked")
  Iterator<Artifact> dependencies = project.getArtifacts().iterator();
  while (dependencies.hasNext()) {
   Artifact artifact = dependencies.next();
   if (isThirdPartyDependency(artifact)) {
    getLog().info("Third party dependency: " + artifact.getId());
    getLog().debug("File loction: " + artifact.getFile&#40;&#41;.getAbsolutePath());
    processDependency(artifact);
   }
  }
  getLog().info(LOG_INFO_LINE);
 }
 
 /**
  * @param artifact project's artifact to check.
  * @return whether this artifact is third party dependency?
  * @throws MojoExecutionException
  * @throws MojoFailureException
  */
 private boolean isThirdPartyDependency(Artifact artifact) throws MojoExecutionException, MojoFailureException {
  getLog().debug("Is artifact " + artifact.getId() + " 3rd party dependency?");
  for (MavenProject module : session.getSortedProjects()) {
   if (module.getId().equals(artifact.getId())) {
    getLog().debug("The artifact " + artifact.getId() + " is the module of this project.");
    return false;
   }
  }
  return true;
 }
 
 /**
  * Copy dependency from the maven local repo to parent project's build folder.
  * @param artifact dependency.
  * @throws MojoExecutionException
  * @throws MojoFailureException
  */
 private void processDependency(Artifact artifact) throws MojoExecutionException, MojoFailureException {
  File destDir = new File&#40;project.getParent(&#41;.getBuild().getDirectory(), "tibco-bw-dependencies");
  if (!destDir.exists()) destDir.mkdirs();
  File destDirAll = new File&#40;destDir, "from-all-tibco-projects"&#41;;
  if (!destDirAll.exists()) destDirAll.mkdirs();
  File destDirProj = new File&#40;destDir, "from-" + project.getArtifactId(&#41;);
  if (!destDirProj.exists()) destDirProj.mkdirs();
  try {
   File destination = new File&#40;destDirProj, artifact.getFile(&#41;.getName());
   getLog().debug("Copy dependency " + artifact.getId() + " to the project specific dir ...");
   FSUtils.copy(artifact.getFile&#40;&#41;, destination);
   getLog().debug("Copy dependency " + artifact.getId() + " to the common dir ...");
   destination = new File&#40;destDirAll, artifact.getFile(&#41;.getName());
   if (!destination.exists()) {
    getLog().debug("Copy " + artifact.getFile&#40;&#41;.getAbsolutePath() + " to " + destination.getAbsolutePath());
    FSUtils.copy(artifact.getFile&#40;&#41;, destination);
   } else {
    getLog().debug("Destination file " + destination.getAbsolutePath() + " already exists!");
   }
  } catch (IOException e) {
   throw new MojoExecutionException("Failed to copy dependency " + artifact.getId() + " from " + artifact.getFile&#40;&#41;.getAbsolutePath() + " to parent project build folder!", e);
  }
 }

}

 

Revise this Paste

Your Name: Code Language: