Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maven and Java "auto build" and Maven profiles #1263

Closed
pyramid-johng opened this issue Feb 6, 2020 · 16 comments
Closed

Maven and Java "auto build" and Maven profiles #1263

pyramid-johng opened this issue Feb 6, 2020 · 16 comments
Assignees
Labels

Comments

@pyramid-johng
Copy link

When you have a POM with multiple profiles, there seems to be no way to tell M2E what profile to build with. It will only build the profile that has "active by default" attached to it. if there is no such profile, the build output is incomplete.

Also, when running MVN build in the terminal on a different profile, the "auto build" may kick in and modify the "target" folder simultaneously with the terminal's MVN execution. This causes many hard-to-diagnose problems, like the "target" folder's contents are incorrect, or the JAR contents is incorrect. I only figured this out when i set my other profile to build to a completely separate "target-xxx" folder.

I can disable Java "auto build" and use the "standard" output folder, and a terminal-based MVN works correctly.

I tried setting the Maven plugins options (e.g. -Pdev), but apparently that does not get used when M2E runs MVN.

I don't really want all my profiles to output to different locations, and I don't want to selectively disable "auto build", so is there a way to tell M2E what profile to use, preferably on a per-folder or per-workspace basis.

Environment
  • Operating System: Windows 10
  • JDK version: JDK 8
  • Visual Studio Code version: 1.41.1
  • Java extension version: 0.55.1
  • Maven for Java: 0.22.1
  • Maven version: 3.6.0
Steps To Reproduce
  1. Have a POM with multiple profiles, e.g. dev and prod, e.g. a JAR package.
  2. Have Java "auto build" enabled.
  3. Have "update configuration" set to automatic.
  4. Have the "dev" profile be "active by default"
  5. Important: to easily observe the issue, the "prod" and "dev" profiles must have some mutually-exclusive content, e.g. the "prod" profile may make minimized JS files that go into the JAR file, and excludes (some of) the files from the "dev" profile, e.g. non-minified JS files.
  6. At this point M2E auto-builds the "dev" profile into "target".
  7. Go to the terminal window and build the "prod" profile via command line.
  8. There will be "interference" with what the "prod" profile does, and the "dev" profile, because "dev" is "active by default" and apparently always gets executed. The most-common outcome is the "prod" execution gets "cleaned" by M2E while it runs the other profile.
  9. Removing "active by default" from "dev" now causes an incomplete build, because any resources etc. that are in the "dev" profile do not execute. This is likely because M2E doesn't have an explicit profile to run (we just deactivated it).
  10. Now if you run the "prod" profile in terminal, everything flows normally.
Current Result
  1. Have to force one of the profiles to be "active by default" so "normal" IDE functions work, e.g. compiled files end up in the "right location".
  2. Have to temporarily disable "auto build" when executing any other profile, then remember to re-enable it afterward :-(
Expected Result
  1. Be able to tell M2E what profile to use when it rebuilds on an auto-build.
  2. Not be forced to have the "makes-IDE-work" profile be active by default.
  3. Honor the Maven Plugins "opts" when it runs maven.
  4. Don't trigger auto-build when the "target" folder changes.
Additional Informations

Is there a way to do this currently?

@snjeza
Copy link
Contributor

snjeza commented Feb 6, 2020

Be able to tell M2E what profile to use when it rebuilds on an auto-build.

@pyramid-johng you can try to add the following property:

activeProfiles=prod

to <your_project>/.settings/org.eclipse.m2e.core.prefs

@GuillaumeDesforges
Copy link

We are missing a setting in this extension to pass parameters to Maven for the build before the run.

@herna
Copy link

herna commented Jul 24, 2020

Any updates on this? It'd be nice a property to specify the active profile.

@jdneo
Copy link
Collaborator

jdneo commented Jan 28, 2021

I found that seems after updating org.eclipse.m2e.core.prefs. An update project action is also needed to be triggered to make the change active.

@testforstephen testforstephen self-assigned this Mar 9, 2021
@AndreaCardamone
Copy link

Be able to tell M2E what profile to use when it rebuilds on an auto-build.

@pyramid-johng you can try to add the following property:

activeProfiles=prod

to <your_project>/.settings/org.eclipse.m2e.core.prefs

Thank you very much. You solved this tedious issue for me!

@CarlPer
Copy link

CarlPer commented Sep 22, 2021

I am having the same issue, that the autobuild randomly during a maven build modifies the generated files.
With eclipse I, nor my team, has ever had this issue.

activeProfiles=prod does not work for us.

@deekshithanand
Copy link

This is a nice feature to have , vs code currently doesnt let to select a profile by default. I am looking for something like this https://www.jetbrains.com/help/idea/work-with-maven-profiles.html#activate_maven_profiles. I can activate a profile when I run via mvn, but am not sure how to do this via autobuild feature used by vscode. Is there any update on this? Its really painful when there are multiple profiles and you need to switch between them or different build configs.

@snjeza
Copy link
Contributor

snjeza commented Jan 11, 2022

@deekshithanand you have to edit <your_project>/.settings/org.eclipse.m2e.core.prefs

@rantoniuk
Copy link

rantoniuk commented Sep 27, 2022

Why does this extension ignore the built-in maven.executable.options": "-Pprofile" setting ?
Also this is just for reference.

@snjeza
Copy link
Contributor

snjeza commented Sep 27, 2022

Why does this extension ignore the built-in maven.executable.options": "-Pprofile" setting ?
Also this is just for reference.

@rantoniuk You may want to take a look at microsoft/vscode-maven#117 (comment)

@mujiannan
Copy link

It hasn't been fixed yet.
Idea is better for enterprise project.

@pbodnar
Copy link

pbodnar commented Apr 10, 2024

Also, when running MVN build in the terminal on a different profile, the "auto build" may kick in and modify the "target" folder simultaneously with the terminal's MVN execution. This causes many hard-to-diagnose problems, like the "target" folder's contents are incorrect, or the JAR contents is incorrect. I only figured this out when i set my other profile to build to a completely separate "target-xxx" folder.

I can disable Java "auto build" and use the "standard" output folder, and a terminal-based MVN works correctly.

Note that this part of the issue, which is not purely about Maven profiles, is reported & discussed in detail here: #1381.

@jdneo
Copy link
Collaborator

jdneo commented Jun 3, 2024

With the latest Language Support for Java(TM) by Red Hat (1.31.0) and latest Extension Pack for Java (0.27.0). You can now change the Maven active profile with a GUI with following steps:

  1. Click the java status bar
    image
  2. Select Open Project Settings
    image
  3. Go to the Maven tab, and type the profile names (comma separated) which you want to enable. For example: profile1,profile2
    image
  4. Click Apply Settings at the bottom of the Project Settings page
    image

@jdneo jdneo closed this as completed Jun 3, 2024
@mucsi96
Copy link

mucsi96 commented Jul 31, 2024

@jdneo Is it possible to persist this in workspace settings? Or where is this persisted?

@jdneo
Copy link
Collaborator

jdneo commented Aug 1, 2024

It's persisted in the project's metadata files, which are per project. By default those files are hidden.

@mucsi96
Copy link

mucsi96 commented Aug 1, 2024

Thx. I think I found a better way.

<profiles>
  <profile>
    <id>local</id>
    <activation>
      <property>
        <name>m2e.version</name>
      </property>
    </activation>
    <dependencies>
      <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
      </dependency>
    </dependencies>
    <build>
      <plugins>
        <plugin>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-maven-plugin</artifactId>
          <configuration>
            <profiles>
              <profile>local</profile>
            </profiles>
          </configuration>
        </plugin>
      </plugins>
    </build>
  </profile>
</profiles>

This way I can share the config with my team without any manual steps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests