Home > Blockchain >  how to create Pom file with my own dependencies using sed
how to create Pom file with my own dependencies using sed

Time:08-31

I have created simple Maven project with pom but would like to add or rather overide ! my own dependencies, especially theese ->

<dependencies>
        <dependency>
            <groupId>pl.bla.abc</groupId>
            <artifactId>example</artifactId>
            <version>3</version>
        </dependency>


<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.0.0.RELEASE</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>


    <distributionManagement>
        <repository>
            <id>nexus</id>
            <url>http://12.0.0.55:8081/repository/maven-releases/</url>
        </repository>
    </distributionManagement>

Im doing this like bellow and im getting error

sed: 1: "s/<dependencies>/<depen ...": bad flag in substitute command: '<'.

Im newbie for Linux so be understanding for me please.

My piece of command line ->

sed 's/<dependencies>/<dependencies>\r\n<!--ghost-->\r\n<dependency>\r\n<groupId>pl.add.ff<\/groupId>\r\n<artifactId>example<\/artifactId>\r\n<version>0.5.0<\/version>\r\n<\/dependency>\r\n<!--ghost-->\r\n<distributionManagement>/<distributionsManagement>\r\n<!--ghost-->\r\n<repository>\r\n<id>nexus<\/id>\r\n<url>http://12.0.0.55:8081/repository/maven-releases/<\/url>\r\n<\/repository\r\n<!--ghost-->/g' pom.xml > pom2.xml

Please help

CodePudding user response:

It might help to show the original/source pom file and what it should look like after you make the substitution?

Regardless, I would suggest it is also easier to start with a 'template' pom file and to use sed to replace explicit values. e.g:

<dependencies>
        <dependency>
            <groupId>{{my.required.group}}</groupId>
            <artifactId>{{my.required.artifact.id}</artifactId>
            <version>{{my.required.version}}</version>
        </dependency>
...

You can then use sed to replace the {{my.required.XXX}} values with the ones you want. In this way you have an always fixed input file making your sed command easier to write and understand.

CodePudding user response:

This pom from generating command

mvn archetype:generate -DgroupId=pl.solution.vpp -DartifactId=PrePom -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -Dpackaging=jar -DgeneratePom=true -DinteractiveMode=false

<?xml version="1.0" encoding="UTF-8"?>

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>pl.solution.vpp</groupId>
  <artifactId>PrePom</artifactId>
  <version>1.0-SNAPSHOT</version>

  <name>PrePom</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-jar-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
        <!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
        <plugin>
          <artifactId>maven-site-plugin</artifactId>
          <version>3.7.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-project-info-reports-plugin</artifactId>
          <version>3.0.0</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

and i would like to have by using sed

->

<?xml version="1.0" encoding="UTF-8"?>

<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>pl.solution.vpp</groupId>
    <artifactId>PrePom</artifactId>
    <version>3</version>
    <packaging>jar</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
        <solid>3</solid>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>2.0.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>pl.solution.vpp</groupId>
            <artifactId>solid</artifactId>
            <version>3</version>
        </dependency>
        <dependency>
            <groupId>com.pi4j</groupId>
            <artifactId>pi4j-core</artifactId>
            <version>1.2</version>
        </dependency>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>3.3.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.7.3</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
    <repositories>
        <repository>
            <id>maven2</id>
            <url>http://repo1.maven.org/maven2</url>
        </repository>
    </repositories>

    <distributionManagement>
        <repository>
            <id>nexus</id>
            <url>http://12.0.0.55:8081/repository/maven-releases/</url>
        </repository>
    </distributionManagement>

</project>
  • Related