【问题标题】:how to create Pom file with my own dependencies using sed如何使用 sed 创建具有我自己的依赖项的 Pom 文件
【发布时间】:2022-10-19 07:19:22
【问题描述】:

我已经用 pom 创建了简单的 Maven 项目,但想添加或覆盖!我自己的依赖项,尤其是这些 ->

<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>

我像下面这样做,我得到了错误

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

我是 Linux 的新手,所以请理解我。

我的命令行->

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

请帮忙

【问题讨论】:

  • 只需更改该 pom 文件...通过编辑器/IDE 为什么使用 sed ???
  • 我想将安装 maven 项目的脚本传递给 raspi,这就是为什么不想编辑它。
  • 在 Raspi 上安装意味着安装生成的 jar 或构建环境?我建议只安装生成的 jar 而不是 Maven ......

标签: maven


【解决方案1】:

显示原始/源 pom 文件以及进行替换后应该是什么样子可能会有所帮助?

无论如何,我建议从“模板”pom 文件开始并使用sed 替换显式值也更容易。例如:

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

然后,您可以使用 sed 将 {{my.required.XXX}} 值替换为您想要的值。这样,您就有了一个始终固定的输入文件,使您的sed 命令更易于编写和理解。

【讨论】:

    【解决方案2】:

    这个来自生成命令的 pom

    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>
    

    我想通过使用 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-09
      • 1970-01-01
      • 2012-02-28
      • 1970-01-01
      • 1970-01-01
      • 2018-03-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多