Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert file separator in maven

I have a property defined like this:

<properties>
    <main.basedir>${project.parent.basedir}</main.basedir>
</properties>

Since I use Windows as OS, it contains backslashes. I want to add this path to a glassfish domain as JVM option (using glassfish maven plugin). The problem is, that asadmin can consume only slash as separator, and all my backslashes keep on disappearing. How can I define a property with exactly the same content with slashes?

like image 941
Gábor Lipták Avatar asked Oct 06 '10 12:10

Gábor Lipták


People also ask

How to set file separator in Java?

separator: Platform dependent default name-separator character as String. For windows, it's '\' and for unix it's '/'. File. separatorChar: Same as separator but it's char.

How do I use a file separator?

A file separator is a character that is used to separate directory names that make up a path to a particular location. This character is operating system specific. On Microsoft Windows, it is a back-slash character (), e.g. C:myprojectmyfilesome. properties.

What is a path delimiter?

The delimiting character is most commonly the slash ("/"), the backslash character ("\"), or colon (":"), though some operating systems may use a different delimiter.


2 Answers

I don't think there is a non-programmatical way to do that. So I suggest a groovy one-liner with the Maven GMaven plugin (GMaven is usually the simplest way to embed programmatic code into a pom):

<plugin>
    <groupId>org.codehaus.gmaven</groupId>
    <artifactId>gmaven-plugin</artifactId>
    <executions>
        <execution>
            <id>setproperty</id>
            <phase>validate</phase>
            <goals>
                <goal>execute</goal>
            </goals>
            <configuration>
                <source>
pom.properties['main.basedir']=project.parent.basedir.absolutePath.replace('\\','/');
                </source>
            </configuration>
        </execution>
    </executions>
</plugin>
like image 83
Sean Patrick Floyd Avatar answered Oct 09 '22 08:10

Sean Patrick Floyd


Just an update to Sean's answer, I have had to make some minor adjustments in order to adapt it to the latest groovy maven plugin version:

<plugin>
          <groupId>org.codehaus.gmaven</groupId>
          <artifactId>groovy-maven-plugin</artifactId>
          <dependencies>
            <dependency>
              <groupId>org.codehaus.groovy</groupId>
              <artifactId>groovy-all</artifactId>
              <version>2.0.1</version>
            </dependency>
          </dependencies>
          <executions>
                <execution>
                    <id>setproperty</id>
                    <phase>validate</phase>
                    <goals>
                        <goal>execute</goal>
                    </goals>
                    <configuration>
                        <source>
        project.properties['basedir']=project.parent.basedir.absolutePath.replace('\\','/');
                        </source>
                    </configuration>
                </execution>
            </executions>
        </plugin>
like image 20
Jorge_B Avatar answered Oct 09 '22 10:10

Jorge_B