Automatically import profiles.xml into maven3 pom.xml

By neokrates, written on July 17, 2010

java   snippet

  • Join date: 11-30-99
  • Posts: 224
View Counter:
  • 154 views
Rate it
Ad
Poll
  • Which test frameworks do you use?

    View Results

    Loading ... Loading ...
Feeds:
  • bodytext bodytext bodytext

Basic code to transfer profiles into pom, thus fixing the issue with maven3 absence of profiles.xml files

Usage:

java -jar maven3-import-profiles.jar -d /your/m2/project/root

 

// Author: Dimitri Uwarov
// Home: http://www.thinkplexx.com/members/neokrates
// Description: basic code to transfer profiles into pom,
// thus fixing the issue with maven3 absence of profiles.xml files
 
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.LineIterator;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;
 
import java.io.*;
 
 
public class MigrateToM3 {
 
     private String extractProfileString(String filename) {
         System.out.println("Extracting profiles.xml file using default 
path: "
                 + filename);
 
         LineIterator it;
         StringBuffer profiles = new StringBuffer();
         boolean profileContentFound = false;
 
         try {
             it = FileUtils.lineIterator(new File(filename), "UTF-8" ;
 
             try {
 
 
                 while (it.hasNext()) {
                     String line = it.nextLine();
 
                     if (!profileContentFound) {
                         int start;
                         if ((start = line.indexOf("<profiles>" ) != -1) {
                             profiles.append(line.substring(start + 10) 
+ "\n" ;
                             profileContentFound = true;
                             continue;
                         }
                     } else {
                         int end;
                         if ((end = line.indexOf("</profiles>" ) != -1) {
                             profiles.append(line.substring(0, end) + "\n" ;
                             break;
                         }
                         profiles.append(line + "\n" ;
                     }
                 }
             } finally {
                 if(!profileContentFound)System.out.println("Profiles 
part was never found in pom.xml, maybe add it manually!?");
                 LineIterator.closeQuietly(it);
             }
 
         } catch (IOException e) {
             System.out.println("Exception during opening of 
profiles.xml file");
             e.printStackTrace();
         }
 
         System.out
                 .println("Extracting profiles from profiles.xml file 
was successfull ");
         return profiles.toString();
     }
 
     private String addProfileString(String filename, String 
profileString) {
         System.out
                 .println("Transferring extracted profiles into 
<profiles> part of pom.xml "
                         + filename);
 
         LineIterator it;
         StringBuffer filetext = new StringBuffer();
         try {
             it = FileUtils.lineIterator(new File(filename), "UTF-8" ;
 
             try {
 
                 boolean profileContentFound = false;
                 while (it.hasNext()) {
                     String line = it.nextLine();
                     filetext.append(line + "\n");
 
                     if (!profileContentFound) {
                         if ((line.indexOf("<!-- START imported from 
profiles.xml to ensure m2 compatibility -->" ) != -1) {
                             System.out.println("The target pom 
"+filename+" already contains profiles. Second append would corrupt the 
pom! " ;
                             return null;
                         }
 
 
                         if ((line.indexOf("<profiles>" ) != -1) {
                             profileContentFound = true;
                             filetext
                                     .append("<!-- START imported from 
profiles.xml to ensure m2 compatibility -->\n" ;
                             filetext.append(profileString);
                             filetext
                                     .append("<!-- END imported from 
profiles.xml to ensure m2 compatibility -->\n" ;
                         }
                     }
 
                 }
             } finally {
                 LineIterator.closeQuietly(it);
             }
 
         } catch (IOException e) {
             System.out.println("Error opening pom.xml" ;
             e.printStackTrace();
         }
 
         System.out
         .println("Transferring profiles from profiles.xml file to 
pom.xml was successfull ");
 
         return filetext.toString();
     }
 
     private void transformMaven2Project(String basedir) {
         String from = basedir + "/profiles.xml";
         String to = basedir + "/pom.xml";
 
         String profiles = extractProfileString(from);
         if(profiles.equals("" ){
             System.out.println(from +"contains no profiles to stransfer");
             return;
         }
         String newPom = addProfileString(to, profiles);
         if(newPom==null) return;
         overwritePom(to, newPom);
 
     }
 
     public void transformMaven2MultimoduleProject(String basedir) {
         File dir = new File(basedir);
         if(!dir.exists()) {
             System.out.println(basedir +" directory does not exist");
             return;
         }
         if(!dir.isDirectory()) {
             System.out.println(basedir +" is not a directory" ;
             return;
         }
         for(File el:dir.listFiles()){
               if(el.isDirectory()&&!el.getName().contains(".git" &&!el.getName().contains(".svn") {
                 transformMaven2Project(el.getAbsolutePath());
             }
 
         }
 
     }
 
 
     private void overwritePom(String to, String newPom) {
         //System.out.println("Wrote out " + to + ".xml" ;
 
         try {
             FileUtils.writeStringToFile(new File(to), newPom);
             System.out.println("Wrote out " + to );
         } catch (IOException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
         }
     }
 
 
     public static void main(String[] args) {
 
         System.out.println( "Starting migration to from m2 to m3");
         // create Options object
         Options options = new Options();
         options.addOption("d", true, "Root directory of m2 project");
 
         CommandLineParser parser = new PosixParser();
         try {
             CommandLine cmd = parser.parse( options, args);
 
             if(cmd.hasOption("d") {
 
                 new 
MigrateToM3().transformMaven2MultimoduleProject(cmd.getOptionValue("d");
 
                 return;
             }
 
         } catch (ParseException exp) {
             System.err.println( "Parsing failed. Reason: " + 
exp.getMessage() );
         }
         HelpFormatter formatter = new HelpFormatter();
         formatter.printHelp( "ant", options );
 
     }
}

Compile

The jar can be build with maven2 or 3. Classic project structure, just pom.xml and MigrateToM3.java.


mvn clean assembly:assembly
mv maven3-import-profiles-1.0-SNAPSHOT-jar-with-dependencies.jar
maven3-import-profiles.jar

 
Following maven pom.xml can be used to build :

<project xmlns="http://maven.apache.org/POM/4.0.0"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/maven-v4_0_0.xsd">;
<modelVersion>4.0.0</modelVersion>
<groupId>maven3-migration</groupId>
<artifactId>maven3-import-profiles</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>migration</name>
<url>http://maven.apache.org</url>;
 
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
 
<dependency>
<groupId>commons-configuration</groupId>
<artifactId>commons-configuration</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
 
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
 
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>MigrateToM3</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
 
</plugins>
</build>
</project>
Be Sociable, Share!
 
Does that help to solve your problem?
VN:F [1.8.5_1061]
Rating: 0 (from 0 votes)
0 votes 'YES'  0 votes 'NO'
TAGS
No tags for this post.
RELATED
Pages
Posts
SOCIAL
Be Sociable, Share!

Leave a Reply