Nice programing

Maven을 사용한 환경 변수

nicepro 2020. 10. 26. 21:03
반응형

Maven을 사용한 환경 변수


Eclipse에서 Maven으로 프로젝트를 포팅했으며 프로젝트가 작동하도록 환경 변수를 설정해야합니다.

Eclipse에서 "실행-> 구성 실행"으로 이동하고 "환경"탭에서 "WSNSHELL_HOME"을 "conf"값으로 설정합니다.

Maven으로 어떻게 할 수 있습니까?

대단히 감사합니다!


명령 줄에서 다음과 같이 전달할 수 있습니다.

mvn -DmyVariable=someValue install

[업데이트] 매개 변수의 순서는 중요 합니다. 명령 전에 옵션을 지정해야합니다 . [/최신 정보]

POM 파일 내에서 시스템 변수 (명령 줄 또는 pom에 지정됨)를으로 ${myVariable}, 환경 변수를 ${env.myVariable}. (수정에 대한 댓글 작성자에게 감사드립니다.)

업데이트 2

좋습니다. 시스템 변수를 테스트에 전달하려고합니다. 내가 가정 했듯이 테스트를 위해 Surefire 플러그인사용하는 경우 가장 좋은 방법은 plugins섹션 에서 필요한 시스템 변수를 pom 내에서 지정하는 것입니다.

<build>
    <plugins>
        ...
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            ...
            <configuration>
                ...
                <systemPropertyVariables>
                    <WSNSHELL_HOME>conf</WSNSHELL_HOME>
                </systemPropertyVariables>
            </configuration>
        </plugin>
        ...
    </plugins>
</build>

-D특성은 확실한-pluging 테스트에에서 신뢰할 수있는 전파되지 않습니다 (이 이클립스와 함께 작동 이유를 모르겠어요). 명령 줄에서 maven을 사용할 때 argLine 속성을 사용하여 속성을 래핑합니다. 이것은 당신의 테스트를 통과합니다

mvn -DargLine="-DWSNSHELL_HOME=conf" test

System.getProperty코드에서 값을 읽는 데 사용 합니다. 의 차이점에 대해이 게시물을 살펴보십시오 .System.getenvSytem.getProperty


properties-maven- plugin이라는 maven 플러그인 이 있는데 이것은 set-system-properties시스템 변수를 설정 하는 목표 제공 합니다. 이러한 속성이 모두 포함 된 파일이있는 경우 특히 유용합니다. 따라서 속성 파일을 읽고 시스템 변수로 설정할 수 있습니다.


bash 스크립트에서 maven 명령을 래핑 할 수 있습니다.

#!/bin/bash

export YOUR_VAR=thevalue
mvn test
unset YOUR_VAR

Maven의 환경 변수는 아래와 같이 설정할 수 있습니다.

http://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html#environmentVariables http://maven.apache.org/surefire/maven-failsafe-plugin/integration-test-mojo.html #환경 변수

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    ...
    <configuration>
        <includes>
            ...
        </includes>
        <environmentVariables>
            <WSNSHELL_HOME>conf</WSNSHELL_HOME>
        </environmentVariables>
    </configuration>
</plugin>

또 다른 해결책은 (또는 창) MAVEN_OPTS에서 ${user.home}/.mavenrc(또는 다른 환경 변수) 를 설정하는 것 %HOME%\mavenrc_pre.bat입니다.

Since Maven 3.3.1 there are new possibilities to set mvn command line parameters, if this is what you actually want:

  • ${maven.projectBasedir}/.mvn/maven.config
  • ${maven.projectBasedir}/.mvn/jvm.config

in your code add:

System.getProperty("WSNSHELL_HOME")

Modify or add value property from maven command:

mvn clean test -DargLine=-DWSNSHELL_HOME=yourvalue

If you want to run it in Eclipse, add VM arguments in your Debug/Run configurations

  • Go to Run -> Run configurations
  • Select Tab Arguments
  • Add in section VM Arguments

-DWSNSHELL_HOME=yourvalue

See image example

you don't need to modify the POM

참고URL : https://stackoverflow.com/questions/5510690/environment-variable-with-maven

반응형