Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to activate maven profile use environment variables?

For some reason, I have to use environment variable instead of passing the system properties to mvn command. So, I use ${env.PROFILE} as the property name, The profile setting in the pom.xml shows as follows:

    <profiles>
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
                <property>
                    <name>${env.PROFILE}</name>
                    <value>dev</value>
                </property>
            </activation>
            <properties>
                <profile.assembly.suffix></profile.assembly.suffix>
            </properties>
        </profile>
        <profile>
            <id>online</id>
            <activation>
                <property>
                    <name>${env.PROFILE}</name>
                    <value>online</value>
                </property>
            </activation>
            <properties>
                <profile.assembly.suffix>-online</profile.assembly.suffix>
            </properties>
        </profile>
    </profiles>

then, I try to activate profile to online, but i failed, the command lines show like these:

E:\Tetris\src\1_Branches\rtm_tetris_zhugan\Server\LobbyServer>set PROFILE=online
E:\Tetris\src\1_Branches\rtm_tetris_zhugan\Server\LobbyServer>echo %PROFILE%
online

E:\Tetris\src\1_Branches\rtm_tetris_zhugan\Server\LobbyServer>mvn help:active-profiles
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for Fusion:LobbyServer:jar:1.0
[WARNING] 'dependencies.dependency.systemPath' for Fusion:Fusion-Core:jar should not point at files within the project directory, ${project.basedir}/../libs/Fusion-Core-2.0.jar will be unresolvable by dependent projects @ line 98, column 25
[WARNING] 'dependencies.dependency.systemPath' for Fusion:common-plugin:jar should not point at files within the project directory, ${project.basedir}/../libs/common-plugin-1.0.jar will be unresolvable by dependent projects @ line 106, column 25
[WARNING] 'dependencies.dependency.(groupId:artifactId:type:classifier)' must be unique: redis.clients:jedis:jar -> duplicate declaration of version (?) @ line 227, column 21
[WARNING] 'dependencyManagement.dependencies.dependency.systemPath' for Fusion:Fusion-Core:jar should not point at files within the project directory, ${project.basedir}/libs/Fusion-Core-2.0.jar will be unresolvable by dependent projects @ Fusion:Fusion-Server:1.0, E:\Tetris\src\1_Branches\rtm_tetris_zhugan\Server\pom.xml, line 64, column 25
[WARNING] 'dependencyManagement.dependencies.dependency.systemPath' for Fusion:common-plugin:jar should not point at files within the project directory, ${project.basedir}/libs/common-plugin-${project.version}.jar will be unresolvable by dependent projects @ Fusion:Fusion-Server:1.0, E:\Tetris\src\1_Branches\rtm_tetris_zhugan\Server\pom.xml, line 72, column 25
[WARNING] 'dependencyManagement.dependencies.dependency.(groupId:artifactId:type:classifier)' must be unique: org.springframework:spring-core:jar -> duplicate declaration of version ${spring.version} @ Fusion:Fusion-Server:1.0, E:\Tetris\src\1_Branches\rtm_tetris_zhugan\Server\pom.xml, line 167, column 21
[WARNING] 'build.plugins.plugin.version' for org.apache.maven.plugins:maven-jar-plugin is missing. @ line 67, column 21
[WARNING] 'dependencyManagement.dependencies.dependency.systemPath' for Fusion:Fusion-Core:jar refers to a non-existing file E:\Tetris\src\1_Branches\rtm_tetris_zhugan\Server\LobbyServer\libs\Fusion-Core-2.0.jar @ Fusion:Fusion-Server:1.0, E:\Tetris\src\1_Branches\rtm_tetris_zhugan\Server\pom.xml, line 64, column 25
[WARNING] 'dependencyManagement.dependencies.dependency.systemPath' for Fusion:common-plugin:jar refers to a non-existing file E:\Tetris\src\1_Branches\rtm_tetris_zhugan\Server\LobbyServer\libs\common-plugin-1.0.jar @ Fusion:Fusion-Server:1.0, E:\Tetris\src\1_Branches\rtm_tetris_zhugan\Server\pom.xml, line 72, column 25
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[INFO]
[INFO] -------------------------< Fusion:LobbyServer >-------------------------
[INFO] Building LobbyServer 1.0
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-help-plugin:3.2.0:active-profiles (default-cli) @ LobbyServer ---
[INFO]
Active Profiles for Project 'Fusion:LobbyServer:jar:1.0':

The following profiles are active:

 - dev (source: Fusion:LobbyServer:1.0)



[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.661 s
[INFO] Finished at: 2019-11-06T12:48:49+08:00
[INFO] ------------------------------------------------------------------------

Then I open the maven debug mode(-X), the properties look like these:

[DEBUG] properties used {spring-data-common.version=1.12.6.RELEASE, file.encoding.pkg=sun.io, env.PROMPT=$P$G, okhttp3.version=3.8.1, java.home=D:\Software\Java\jdk1.8.0_121\jre, env.CLION=D:\Software\CLion 2019.1.4\bin;, classworlds.conf=E:\Tetris\src\1_Branches\rtm_tetris_zhugan\Server\BuildTool\maven\bin\..\bin\m2.conf, profile.assembly.suffix=, java.endorsed.dirs=D:\Software\Java\jdk1.8.0_121\jre\lib\endorsed, env.USERNAME=zhengtongshan, env.PHPSTORM=D:\Software\JetBrains\PhpStorm 2019.2.2\bin;, servlet.version=3.1.0, sun.os.patch.level=, java.vendor.url=http://java.oracle.com/, env.COMPUTERNAME=DESKTOP-R7DT0R0, env.=C:=C:\Users\zhengtongshan, java.version=1.8.0_121, commons-beanutils.version=1.9.3, env.DRIVERDATA=C:\Windows\System32\Drivers\DriverData, fastjson.version=1.2.62, spring-data-redis.version=1.8.4.RELEASE, java.vendor.url.bug=http://bugreport.sun.com/bugreport/, env.USERPROFILE=C:\Users\zhengtongshan, apache.httpclient.version=4.5.3, jedis.version=2.9.0, user.name=zhengtongshan, sun.io.unicode.encoding=UnicodeLittle, sun.jnu.encoding=GBK, java.runtime.name=Java(TM) SE Runtime Environment, env.LOCALAPPDATA=C:\Users\zhengtongshan\AppData\Local, env.COMMONPROGRAMW6432=C:\Program Files\Common Files, java.specification.name=Java Platform API Specification, user.timezone=Asia/Shanghai, user.script=, path.separator=;, env.MAVEN_CMD_LINE_ARGS=-e  -X -f E:\Tetris\src\1_Branches\rtm_tetris_zhugan\Server\BuildTool\../RoomServer/pom.xml clean assembly:assembly, env.PROCESSOR_IDENTIFIER=Intel64 Family 6 Model 158 Stepping 10, GenuineIntel, file.encoding=GBK, sun.java.command=org.codehaus.plexus.classworlds.launcher.Launcher -e -X -f E:\Tetris\src\1_Branches\rtm_tetris_zhugan\Server\BuildTool\../RoomServer/pom.xml clean assembly:assembly, env.NUMBER_OF_PROCESSORS=12, env.APPDATA=C:\Users\zhengtongshan\AppData\Roaming, env.WINDIR=C:\Windows, java.io.tmpdir=C:\Users\ZHENGT~1\AppData\Local\Temp\, user.language=zh, line.separator=
, env.COMMONPROGRAMFILES=C:\Program Files\Common Files, java.vm.info=mixed mode, sun.desktop=windows, java.vm.specification.name=Java Virtual Machine Specification, project.reporting.outputEncoding=UTF-8, env.PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC, env.WDIR=E:\, env.ONEDRIVE=C:\Users\zhengtongshan\OneDrive, env.USERDOMAIN_ROAMINGPROFILE=DESKTOP-R7DT0R0, commons-io.version=2.5, trove4j.version=3.0.3, env.LOGONSERVER=\\DESKTOP-R7DT0R0, env.PSMODULEPATH=C:\Program Files\WindowsPowerShell\Modules;C:\Windows\system32\WindowsPowerShell\v1.0\Modules, java.awt.printerjob=sun.awt.windows.WPrinterJob, commons-codec.version=1.9, env.JAVACMD=D:\Software\Java\jdk1.8.0_121\bin\java.exe, env.JVMCONFIG=\.mvn\jvm.config, env.PUBLIC=C:\Users\Public, env.USERDOMAIN=DESKTOP-R7DT0R0, env.PROCESSOR_LEVEL=6, env.PROGRAMFILES(X86)=C:\Program Files (x86), env.=EXITCODE=00000000, env.FILE_ARG=E:\Tetris\src\1_Branches\rtm_tetris_zhugan\Server\BuildTool\../RoomServer/pom.xml, os.name=Windows 10, java.specification.vendor=Oracle Corporation, env.TMP=C:\Users\ZHENGT~1\AppData\Local\Temp, java.vm.name=Java HotSpot(TM) 64-Bit Server VM, env.OS=Windows_NT, java.library.path=D:\Software\Java\jdk1.8.0_121\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;D:\Software\Java\jdk1.8.0_121\bin;C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;D:\Software\TortoiseSVN\bin;D:\Software\Git\cmd;E:\FusionDB\mongodb-3.2.17\bin;E:\FusionDB\Redis-x64-3.2.100;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;D:\Software\Cygwin\bin;D:\Software\Calibre2\;D:\Software\apache-ant-1.8.2\bin\;D:\Software\apache-maven-3.6.2-bin\apache-maven-3.6.2\bin;D:\Software\Python3.6\Scripts\;D:\Software\Python3.6\;C:\Users\zhengtongshan\AppData\Local\Microsoft\WindowsApps;;D:\Software\JetBrains\IntelliJ IDEA 193.4386.10\bin;;D:\Software\JetBrains\JetBrains Rider 2019.1.2\bin;;D:\Software\CLion 2019.1.4\bin;;D:\Software\JetBrains\PhpStorm 2019.2.2\bin;;., env.PROGRAMW6432=C:\Program Files, env.PATH=D:\Software\Java\jdk1.8.0_121\bin;C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;D:\Software\TortoiseSVN\bin;D:\Software\Git\cmd;E:\FusionDB\mongodb-3.2.17\bin;E:\FusionDB\Redis-x64-3.2.100;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;D:\Software\Cygwin\bin;D:\Software\Calibre2\;D:\Software\apache-ant-1.8.2\bin\;D:\Software\apache-maven-3.6.2-bin\apache-maven-3.6.2\bin;D:\Software\Python3.6\Scripts\;D:\Software\Python3.6\;C:\Users\zhengtongshan\AppData\Local\Microsoft\WindowsApps;;D:\Software\JetBrains\IntelliJ IDEA 193.4386.10\bin;;D:\Software\JetBrains\JetBrains Rider 2019.1.2\bin;;D:\Software\CLion 2019.1.4\bin;;D:\Software\JetBrains\PhpStorm 2019.2.2\bin;, library.jansi.path=E:\Tetris\src\1_Branches\rtm_tetris_zhugan\Server\BuildTool\maven\bin\..\lib\jansi-native\windows64, java.class.version=52.0, maven.multiModuleProjectDirectory=E:\Tetris\src\1_Branches\rtm_tetris_zhugan\Server\BuildTool, javaxValidation.version=1.1.0.Final, env.HOMEDRIVE=C:, env.SYSTEMROOT=C:\Windows, spring.version=4.3.9.RELEASE, env.COMSPEC=C:\Windows\system32\cmd.exe, sun.boot.library.path=D:\Software\Java\jdk1.8.0_121\jre\bin, project.build.sourceEncoding=UTF-8, env.SYSTEMDRIVE=C:, env.PROCESSOR_REVISION=9e0a, sun.management.compiler=HotSpot 64-Bit Tiered Compilers, java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment, user.variant=, mongo-java-driver.version=3.3.0, netty.version=4.1.6.Final, junit.version=RELEASE, env.PROGRAMFILES=C:\Program Files, java.vm.specification.version=1.8, env.POM_DIR=E:\Tetris\src\1_Branches\rtm_tetris_zhugan\Server\RoomServer, env.PROGRAMDATA=C:\ProgramData, slf4j.version=1.7.25, awt.toolkit=sun.awt.windows.WToolkit, env.MAVEN_HOME=E:\Tetris\src\1_Branches\rtm_tetris_zhugan\Server\BuildTool\maven\bin\.., sun.cpu.isalist=amd64, sun.stderr.encoding=ms936, env.MAVEN_PROJECTBASEDIR=E:\Tetris\src\1_Branches\rtm_tetris_zhugan\Server\BuildTool, java.ext.dirs=D:\Software\Java\jdk1.8.0_121\jre\lib\ext;C:\Windows\Sun\Java\lib\ext, os.version=10.0, env.ERROR_CODE=0, user.home=C:\Users\zhengtongshan, env.PROFILE=online, java.vm.vendor=Oracle Corporation, env.JETBRAINS RIDER=D:\Software\JetBrains\JetBrains Rider 2019.1.2\bin;, env.JAVA_HOME=D:\Software\Java\jdk1.8.0_121, user.dir=E:\Tetris\src\1_Branches\rtm_tetris_zhugan\Server\BuildTool, maven.conf=E:\Tetris\src\1_Branches\rtm_tetris_zhugan\Server\BuildTool\maven\bin\../conf, env.COMMONPROGRAMFILES(X86)=C:\Program Files (x86)\Common Files, log4j.version=2.8.2, sun.cpu.endian=little, spring-data-mongo.version=1.10.4.RELEASE, env.ALLUSERSPROFILE=C:\ProgramData, env.CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher, env.PROCESSOR_ARCHITECTURE=AMD64, java.vm.version=25.121-b13, java.class.path=E:\Tetris\src\1_Branches\rtm_tetris_zhugan\Server\BuildTool\maven\bin\..\boot\plexus-classworlds-2.5.2.jar, env.=::=::\, org.slf4j.simpleLogger.defaultLogLevel=debug, env.HOMEPATH=\Users\zhengtongshan, env.EXEC_DIR=E:\Tetris\src\1_Branches\rtm_tetris_zhugan\Server\BuildTool, os.arch=amd64, maven.build.version=Apache Maven 3.5.0 (ff8f5e7444045639af65f6095c62210b5713f426; 2017-04-04T03:39:06+08:00), env.SESSIONNAME=Console, guava.version=19.0, sun.java.launcher=SUN_STANDARD, java.vm.specification.vendor=Oracle Corporation, file.separator=\, gson.version=2.7, java.runtime.version=1.8.0_121-b13, sun.boot.class.path=D:\Software\Java\jdk1.8.0_121\jre\lib\resources.jar;D:\Software\Java\jdk1.8.0_121\jre\lib\rt.jar;D:\Software\Java\jdk1.8.0_121\jre\lib\sunrsasign.jar;D:\Software\Java\jdk1.8.0_121\jre\lib\jsse.jar;D:\Software\Java\jdk1.8.0_121\jre\lib\jce.jar;D:\Software\Java\jdk1.8.0_121\jre\lib\charsets.jar;D:\Software\Java\jdk1.8.0_121\jre\lib\jfr.jar;D:\Software\Java\jdk1.8.0_121\jre\classes, env.CMAKE_C_COMPILER=D:/Software/Cygwin/bin/gcc.exe, jackson.version=2.8.9, maven.version=3.5.0, env.TEMP=C:\Users\ZHENGT~1\AppData\Local\Temp, user.country=CN, maven.home=E:\Tetris\src\1_Branches\rtm_tetris_zhugan\Server\BuildTool\maven\bin\.., maven.compiler.target=1.8, env.=E:=E:\Tetris\src\1_Branches\rtm_tetris_zhugan\Server\BuildTool, commons-lang3.version=3.6, env.INTELLIJ IDEA=D:\Software\JetBrains\IntelliJ IDEA 193.4386.10\bin;, commons-pool2.version=2.4.2, java.vendor=Oracle Corporation, env.CLASSWORLDS_JAR="E:\Tetris\src\1_Branches\rtm_tetris_zhugan\Server\BuildTool\maven\bin\..\boot\plexus-classworlds-2.5.2.jar", protobuf.version=3.2.0, maven.compiler.source=1.8, java.specification.version=1.8, sun.arch.data.model=64, argLine=-Dfile.encoding=UTF-8}

We can find the properties on above properties list:

env.PROFILE=online, profile.assembly.suffix=,

It seems the "PROFILE" variable has been setted in envrionment. However, the "online" profile is not activated. What's the problem? thanks.

like image 428
pcloves Avatar asked Nov 06 '19 05:11

pcloves


People also ask

Do we need to set environment variables for Maven?

Note that if you want to use Maven, you need to have Java installed and an environment variable set up. Open Google and search for maven download.

Which element activates a profile by default in Maven?

Profiles can be activated in the Maven settings, via the <activeProfiles> section. This section takes a list of <activeProfile> elements, each containing a profile-id inside. Profiles listed in the <activeProfiles> tag would be activated by default every time a project use it.


2 Answers

Replace <name>${env.PROFILE}</name> to <name>env.PROFILE</name> and try running mvn help:active-profiles, you should get the online profile.

C:\Sachin\data\development\app_code\spring-security-demo>mvn help:active-profiles
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building spring-security-demo 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-help-plugin:3.1.1:active-profiles (default-cli) @ spring-security-demo ---
[INFO]
Active Profiles for Project 'com.security.example:spring-security-demo:jar:0.0.1-SNAPSHOT':

The following profiles are active:

 - online (source: com.security.example:spring-security-demo:0.0.1-SNAPSHOT)

Below is the updated profile tag

<profiles>
    <profile>
        <id>dev</id>
        <activation>
            <activeByDefault>true</activeByDefault>
            <property>
                <name>env.PROFILE</name>
                <value>dev</value>
            </property>
        </activation>
        <properties>
            <profile.assembly.suffix></profile.assembly.suffix>
        </properties>
    </profile>
    <profile>
        <id>online</id>
        <activation>
            <property>
                <name>env.PROFILE</name>
                <value>online</value>
            </property>
        </activation>
        <properties>
            <profile.assembly.suffix>-online</profile.assembly.suffix>
        </properties>
    </profile>
</profiles>

You could also use mvn help:all-profiles which would list all profiles with its Active status. For ex. for your example if we have set the PROFILE value to online running mvn help:all-profiles should display as below

[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building spring-security-demo 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-help-plugin:3.1.1:all-profiles (default-cli) @ spring-security-demo ---
[INFO] Listing Profiles for Project: com.security.example:spring-security-demo:jar:0.0.1-SNAPSHOT
  Profile Id: online (Active: true , Source: pom)
  Profile Id: dev (Active: false , Source: pom)

I tested it using maven 3.2.5 in windows 10 OS.

like image 129
Sachin Avatar answered Oct 09 '22 12:10

Sachin


You are very close. When referencing environment variables, you shouldn't use ${...} but simply env.VARIABLE.

A slightly adjusted example to illustrate what I mean:

SET PROFILE=online

POM:

<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>org.example</groupId>
  <artifactId>profiles</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>
  <name>profiles</name>
  <url>http://maven.apache.org</url>
  <profiles>
    <profile>
      <id>prof-1</id>
      <activation>
         <activeByDefault>true</activeByDefault>
         <property>
           <name>env.PROFILE</name>
           <value>dev</value>
         </property>
      </activation>
      <properties>
         <profile.assembly.suffix>-dev</profile.assembly.suffix>
      </properties>            
    </profile>
    <profile>
      <id>prof-2</id>
      <activation>
         <property>
            <name>env.PROFILE</name>
            <value>online</value>
         </property>
      </activation>
      <properties>
         <profile.assembly.suffix>-online</profile.assembly.suffix>
      </properties>
    </profile>
  </profiles>
<build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>1.8</version>
        <executions>
          <execution>
            <id>print-profile</id>
            <phase>validate</phase>
            <configuration>
              <target>
                <echo message="profile assembly: ${profile.assembly.suffix}"/>
              </target>
            </configuration>
            <goals>
              <goal>run</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>  
</project>

OUTPUT:

mvn validate
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------< org.example:profiles >------------------------
[INFO] Building profiles 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-antrun-plugin:1.8:run (print-profile) @ profiles ---
[INFO] Executing tasks

main:
     [echo] profile assembly: -online
[INFO] Executed tasks
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.624 s
[INFO] Finished at: 2019-11-06T10:10:18+01:00
[INFO] ------------------------------------------------------------------------
like image 2
Daniel Avatar answered Oct 09 '22 12:10

Daniel