【问题标题】:How to pass argument to maven from shell script如何从shell脚本将参数传递给maven
【发布时间】:2019-09-29 15:09:47
【问题描述】:

我正在使用一个 shell 脚本,它接收用户的输入并将其发送到 maven pom.xml 并通过 java 代码进一步使用此参数。

And this the steps

1) Shell script is 

   #!/bin/bash
   ### Input password as hidden charactors ###
   read -s -p "Enter Password: "  pswd
   # passing this argument that called my java code.
   /bin/su $USER -c "${JAVA} -jar ${JAR}" $pswd

2) how can I pass the argument $pswd in my maven pom.xml

     <build>
<plugins>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-jar-plugin</artifactId>
  </plugin>
  <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>shade</goal>
        </goals>
        <configuration>
          <transformers>
            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
              <mainClass>com.abc.abcd.abcde.server.pswd.Password</mainClass>
            </transformer>
          </transformers>
          <filters>
            <filter>
              <artifact>*:*</artifact>
              <excludes>
                <exclude>META-INF/*.SF</exclude>
                <exclude>META-INF/*.DSA</exclude>
                <exclude>META-INF/*.RSA</exclude>
              </excludes>
            </filter>
          </filters>
        </configuration>
      </execution>
    </executions>
  </plugin>
</plugins>

这是我的java代码

   public final class Password
  {

    private Password()
   {
   }

   public static void main(final String[] args)
   {
    System.out.println(args[0]);
   }
   }
Enter Password: Exception in thread "main" 
java.lang.ArrayIndexOutOfBoundsException: 0

那么我怎样才能将 $pswd 参数传递给 maven pom.xml 和相同的 参数将在 Java 代码中使用 ???????

【问题讨论】:

    标签: java shell maven


    【解决方案1】:

    你应该在-jar之前传递参数:

    /bin/su $USER -c "${JAVA} $pswd -jar ${JAR}"
    

    【讨论】:

    • @ShubhamGoswami 您是从 maven 运行 jar 还是仅使用 maven 进行组装?
    • 放完这个我得到 /bin/su $USER -c "${JAVA} $pswd -jar ${JAR}" Enter Password: Error: Could not find or load main class
    【解决方案2】:
       That is the right way
      /bin/su $USER -c "${JAVA} -jar ${JAR} $pswd"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-08
      • 1970-01-01
      • 1970-01-01
      • 2021-04-16
      • 1970-01-01
      • 2014-11-27
      相关资源
      最近更新 更多