IT story

Maven의 pom.xml에서 쉘 명령을 실행하고 싶습니다.

hot-time 2020. 8. 21. 19:46
반응형

Maven의 pom.xml에서 쉘 명령을 실행하고 싶습니다.


Maven으로 Linux 셸 명령을 실행하고 싶습니다. 내가 시도한 것은 다음과 같습니다.

<plugin>  
  <groupId>org.codehaus.mojo</groupId> 
  <artifactId>exec-maven-plugin</artifactId> 
  <version>1.1.1</version> 
  <executions>
    <execution>
      <goals>
        <goal>exec</goal> 
      </goals>
    </execution>
  </executions>
  <configuration>
    <executable>hostname</executable> 
  </configuration>
</plugin>

나를 위해 일한 것은 다음과 같습니다.

<plugin>
  <artifactId>exec-maven-plugin</artifactId>
  <groupId>org.codehaus.mojo</groupId>
  <executions>
    <execution><!-- Run our version calculation script -->
      <id>Version Calculation</id>
      <phase>generate-sources</phase>
      <goals>
        <goal>exec</goal>
      </goals>
      <configuration>
        <executable>${basedir}/scripts/calculate-version.sh</executable>
      </configuration>
    </execution>
  </executions>
</plugin>


여기서 문제는 예상되는 것이 무엇인지 모른다는 것입니다 . 현재 설정에서 명령 줄에서 플러그인을 호출하면 다음과 같이 작동합니다.

$ mvn exec : exec
[정보] 프로젝트 검색 중 ...
[정보] ----------------------------------------------- -------------------------
[정보] 건물 Q3491937
[정보] 작업 세그먼트 : [exec : exec]
[정보] ----------------------------------------------- -------------------------
[정보] [exec : exec {execution : default-cli}]
[정보] 노트북
[정보] ----------------------------------------------- -------------------------
[정보] 빌드 성공
[정보] ----------------------------------------------- -------------------------
...

전역 configuration이 사용되고 hostname명령이 실행됩니다 ( laptop내 호스트 이름 임). 즉, 플러그인이 예상대로 작동합니다.

이제 플러그인을 빌드의 일부로 실행 하려면 특정 단계에서 목표 바인딩 해야합니다 . 예를 들어 바인딩하려면 다음을 수행하십시오 compile.

  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.1.1</version>
    <executions>
      <execution>
        <id>some-execution</id>
        <phase>compile</phase>
        <goals>
          <goal>exec</goal>
        </goals>
      </execution>
    </executions>
    <configuration>
      <executable>hostname</executable>
    </configuration>
  </plugin>

그리고:

$ mvn 컴파일
[정보] 프로젝트 검색 중 ...
[정보] ----------------------------------------------- -------------------------
[정보] 건물 Q3491937
[정보] 작업 세그먼트 : [컴파일]
[정보] ----------------------------------------------- -------------------------
[정보] [자원 : 자원 {실행 : 기본 자원}]
[정보] 'UTF-8'인코딩을 사용하여 필터링 된 리소스를 복사합니다.
[정보] 존재하지 않는 resourceDirectory / home / pascal / Projects / Q3491937 / src / main / resources 건너 뛰기
[정보] [컴파일러 : 컴파일 {실행 : 기본 컴파일}]
[정보] 컴파일 할 항목이 없습니다. 모든 클래스가 최신 상태입니다.
[정보] [exec : exec {execution : some-execution}]
[정보] 노트북
[정보] ----------------------------------------------- -------------------------
[정보] 빌드 성공
[정보] ----------------------------------------------- -------------------------
...

Note that you can specify a configuration inside an execution.


Solved. The problem is, executable is working in a different way in Linux. If you want to run .sh file, you should write in the way below. Write it in pom.xml

    <plugin>
        <artifactId>exec-maven-plugin</artifactId>
    <version>1.6.0</version>
        <groupId>org.codehaus.mojo</groupId>
        <executions>
            <execution><!-- Run our version calculation script -->
                <id>Renaming build artifacts</id>
                <phase>package</phase>
                <goals>
                    <goal>exec</goal>
                </goals>
                <configuration>
                    <executable>bash</executable>
            <commandlineArgs>handleResultJars.sh</commandlineArgs>
                </configuration>
            </execution>
        </executions>
    </plugin>

2 Options:

  1. You want to exec a command from maven without binding to any phase, you just type the command and maven runs it, you just want to maven to run something, you don't care if we are in compile/package/... Let's say I want to run npm start with maven, you can achieve it with the below:

mvn exec:exec -Pstart-node

For that you need the below maven section

  <profiles>
    <profile>
      <id>start-node</id>
      <build>
        <plugins>
          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.3.2</version>
            <executions>
              <execution>
                <goals>
                  <goal>exec</goal>
                </goals>
              </execution>
            </executions>
            <configuration>
              <executable>npm</executable>
              <arguments><argument>start</argument></arguments>
            </configuration>
          </plugin>
        </plugins>
      </build>
    </profile>

  </profiles>
  1. You want to run an arbitrary command from maven while you are at a specific phase, for example when I'm at install phase I want to run npm install you can do that with:

mvn install

And for that to work you would need the below section:

  <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>1.3.2</version>
    <executions>

      <execution>
        <id>npm install (initialize)</id>
        <goals>
          <goal>exec</goal>
        </goals>
        <phase>initialize</phase>
        <configuration>
          <executable>npm</executable>
          <arguments>
            <argument>install</argument>
          </arguments>
        </configuration>
      </execution>

참고URL : https://stackoverflow.com/questions/3491937/i-want-to-execute-shell-commands-from-mavens-pom-xml

반응형