Does intellij run maven as a different user? - java

I have configured the enforcer to check for some properties. These are exported as environment variables in my .profile file in my home dir. The build from the console runs fine.
The build from intellij (doubleclicking the goals in the maven menu) fails, as enforcer cannot find the properties...What is going wrong here and how to fix it?
My first guess is that intellij is running maven as a different user but I'm not sure how to verify it or what to do about it.
NOTE: I am not asking for a workaround. One would be to set the properties in maven Runner settings of intellij, a second - to set the properties in /etc/profile. But I would like to avoid them.
EDIT : I have tried restarting intellij, the X server and the machine - to no effect on the enforcer. printenv in a fresh console shows my variables as expected. Starting intellij from the same console leads to the same build failure.
Thank you.

Each program has its own environment. If you change the environment settings in .profile or the like, it doesn't change the setting for running programs. When one program runs another program it inherits the settings/environment of its parent. It doesn't load the settings again.
What you could be finding is that changes you make after IntelliJ has started, have no effect from programs IntelliJ runs. The same thing would happen if you opened a command window, changed the settings and run a new programs (the shell would keep the settings it had when it started) To fix this you need to re-start IntelliJ. If you run it from the command line, make sure its a new window.
This unit test should dump the environment.
#Test
public void dumpEnv() {
for (Map.Entry<String, String> entry : new TreeMap<String, String>(System.getenv()).entrySet()) {
System.out.println(entry);
}
}

.profile is only executed when you log on using KSH. If you use BASH, it has no effect.
.bashrc (if you use BASH) is a much more suitable place because it's executed every time you start a shell (shell scripts, new terminal windows, etc).
For KSH, .kshrc has the same effect.
To make sure the variables are there, use set (no options) to list all of them or set|greppattern to search. As soon as they show up in a new terminal window, they should be there from inside your IDE, too.
[EDIT] If set can see them, so can IntelliJ. The only thing left is that IntelliJ could clean the environment when it starts Maven.
To test that, rename mvn to mvn.orig and create a new script mvn with this content:
#!/bin/bash
set > $HOME/mvn_env.log
mvn.orig "$#"
This script dumps the environment as mvn sees it to $HOME/mvn_env.log. If the variables are missing, then IntelliJ cleans the environment.

Related

How can I see the actual commands (java) running in Intellij Idea?

I use IntelliJ IDEA CE in two environments (CentOS and Ubuntu).
The two share a project via GitHub.
In CentOS, the project is regarded as Java project? with a file build.gradle.
In the other, the project is fully managed as Gradle-Java project.
When I run a main method in CentOS, IDE just runs Java (after some compiles if are required)
On the other hand, when I run the same main method in Ubuntu, IDE activates Gradle and run that.
I think this difference happens from the module structures.. which I have not tried though....
(Am I wrong?)
One question I really want to know here is,
in CentOS, when I run a code, it shows how IDE ran the code in the Run window, like...
java [too many options] foo.target.Hoge
If I copy and paste this line, I can activate foo.target.Hoge anywhere, even where I do not set CLASSPATH because [too many options] contain that.
I always do it (I am afraid if I should not though)
However, in Ubuntu (Gradle-based environment)
the windows show only
time PM: Executing task 'Hoge.main()'...
I can not copy, paste and run it...
I want to know where I can found the actual command activated.
or.... using Gradle.. are there any simpler ways?? like
gradle run Java Hoge ...
When I run a main method in CentOS, IDE just runs Java (after some compiles if are required) On the other hand, when I run the same main method in Ubuntu, IDE activates Gradle and run that.
For Gradle-based projects this behaviour is controlled in Settings (Preferences on macOS) | Build, Execution, Deployment | Build Tools | Gradle | Build and run using option. When Gradle is set IDE uses Gradle to run and build the project. When IntelliJ is set IDE uses it's own builder and Run/Debug Configurations to run.

how do you debug java annotation processors using intellij?

How do you debug java annotation processors using intellij?
Preferably using IDEA IntelliJ. I tried setting a breakpoint inside the processor and running but it did not break.
If you really need to debug an annotation processor, it might be better to run the annotation processor from the command line rather than within your IDE with debugging enabled and attach to that using your IDE's debugger.
If running javac directly, you can debug this by specifying the following extra parameters:
javac -J-Xdebug -J-Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 ... (usual javac parameters go here)
If running Maven, use mvndebug instead of the standard mvn command - Maven runs the compiler in-process.
If running Ant, add the following to the ANT_OPTS environment variable before running:
-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
With all these executions, the javac, Ant or Maven process will wait for you to attach your debugger before it actually starts executing. IntelliJ instructions for this are here. For Eclipse, here.
This tutorial is written for an Android project. Main module name is "app" as usual. The project contains a submodule called "annotation" which is subdependency of "app". "app" module runs annotation processing with gradle declaration
apt project(':annotation') .
SIMPLE VERSION
(run compilation from terminal and attach from IDE)
[REQUIRED] Add a new project configuration "+" -> "Remote". Check "Single instance only". All other settings are generated automatically. Leave <whole project> as the classpath. Port should be left as the default 5005.
[REQUIRED] Make sure you stop all gradle instances by calling: ./gradlew --stop
[REQUIRED] Run the command : ./gradlew --no-daemon -Dorg.gradle.debug=true :app:clean :app:compileDebugJavaWithJavac
Run the APT project configuration in debug mode as fast as possible :)
[HINT] We start with an EMPTY gradle.properties file
[HINT] DO NOT USE gradle daemon ( --no-daemon / org.gradle.daemon=false option )
[HINT] Run gradle in debug mode ( org.gradle.debug=true option )
[HINT] Run app's module compilation not the processor's module compilation (app's compilation runs annotation processing!)
We DO NOT normally add any Java compiler settings in Android Studio (i.e. File -> other settings -> Default settings)
EXTENDED VERSION (use gradle.properties)
Add the following to your gradle.properties file:
org.gradle.daemon=false
org.gradle.debug=true
Run the compilation from terminal:
./gradlew :app:clean :app:compileDebugJavaWithJavac
ADVANCED VERSION (just press debug in IDE)
Add a bash script to your project main dir (e.g. compile.sh)
#!/bin/bash
./gradlew :app:clean :app:compileDebugJavaWithJavac &
Remember about the '&' sign for background processing.
Go to APT configuration settings we created in step 1 and add a Before launch configuration. Select Run external tool.
Add the path to the compile.sh script we created earlier.
Warning
Messing up gradle compilation, NullPointer exceptions during compilation etc. sometimes result in AndroidStudio being stuck (frozen on gradle refresh stage). If you cannot stop gradle from the IDE then use this command in the terminal:
ps -A | grep gradle | awk '{ print $1; }' | xargs kill -9
Turning off debug option during project refresh sometimes helps Android Studio to come back to the right track.
Follow these steps, These worked for me on android studio for gradle project:-
1).In gradle.properties add following lines
org.gradle.daemon=true
org.gradle.jvmargs=-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005
2).Edit Build COnfiguration and add Remote Configration
3).Run recently created run configuration APT.
4).Add break point in annotation processing code and build overall project
It is possible to run javac and debug it, as indicated higher. However in my case it was tedious to write the complete classpath, so I wanted to leave this to IDEA. So in the module where I wanted to apply my annotation processor, just create a class with main method.
public static void main(String[] args) {
com.sun.tools.javac.Main.main("-proc:only",
"-processor", "my.pkgs.MyAnnotationProcessor",
"my/pkgs/any/ClassIWantProcess.java");
}
For this to work you need to add $JAVA_HOME/lib/tools.jar to your SDK's JARs (by default it is not there). This is the same reason why appservers compiling JSPs need this JAR on their classpath - they need the compiler.
Then just set proper working directory for your run configuration (so the relative path to the java file is correct), set your break-point into the processor and debug at your will!
Benefit - classpath is set already by IDEA and used by the "inner" javac, because here it is not a separate process. I believe it's possible to translate it to other IDEs too.
For a Maven project, this post, which explains the following steps in a little more detail, worked for me:
Add "Remote" run configuration and set "port" to 8000.
Issue the command mvnDebug clean install from the project's directory (on the command line).
Run the run configuration. In order to start a new session after the processes quit, repeat from (2).
Remember to run mvn install on the project's dependencies when they change (e.g. if the annotation processor is in a different artifact than the project you are debugging it from).
I found the following resource that can help you: http://code.google.com/p/acris/wiki/AnnotationProcessing_DebuggingEclipse
The guy explains step-by-step how to debug annotation processors using Eclipse.
Annotation processing occurs during compilation, so normal debugging won't work. If you want to debug it in the context of you project, you can use IntelliJ remote debugging, while having Gradle or Maven in debug mode. Then you can put breakpoints in the Annotation Processor's files.
See Debugging an Annotation Processor in any project.
Disclaimer: I wrote the post.
Debugging an annotation processor with IntelliJ IDEA and Gradle
Set a custom VM option -Dcompiler.process.debug.port=5005: press Ctrl + Shift + A and select Edit Custom VM Options... in the list of actions to add a custom VM option then restart the IDE.
Create a remote debug configuration with default parameters: Run -> Edit Configurations... -> Add New Configuration (Alt + Insert) -> Remote.
Set breakpoints.
Build with Gradle from the terminal: $ ./gradlew --no-daemon -Dorg.gradle.debug=true clean build (it's okay if the execution of the command is frozen, don't terminate a process).
Debug the remote debug configuration within the IDE (see step 3): select a suitable remote debug configuration and press Shift + F9.
Hope it helps somebody :)

Environment variables in Eclipse

I am able to run a sample hadoop program from the command prompt and am trying to run the same program from Eclipse, so that I can debug it and understand it better.
For the command line program, some environment variables are set in the .bashrc and the same are being read as System.getenv().get("HADOOP_MAPRED_HOME") in the hadoop program. But, when I am running a java program with System.getenv().get("HADOOP_MAPRED_HOME"), from Eclipse I am getting null.
I tried passing -DHADOOP_MAPRED_HOME=test to VM parameters in the runtime configurations from Eclipse, but still getting null in the standalone program. How to make the environment variables visible within Eclipse? When I iterate through System.getenv() in Eclipse, I see lot of variables like DISPLAY, USER, HOME and others. Where are they set? I am using Ubuntu 11.04.
You can also define an environment variable that is visible only within Eclipse.
Go to Run -> Run Configurations... and Select tab "Environment".
There you can add several environment variables that will be specific to your application.
I've created an eclipse plugin for this, because I had the same problem.
Feel free to download it and contribute to it.
It's still in early development, but it does its job already for me.
https://github.com/JorisAerts/Eclipse-Environment-Variables
The .bashrc file is used for setting variables used by interactive login shells. If you want those environment variables available in Eclipse you need to put them in /etc/environment.
You can set the Hadoop home directory by sending a -Dhadoop.home.dir to the VM. To send this parameters to all your application that you execute inside eclipse, you can set them in Window->Preferences->Java->Installed JREs-> (select your JRE installation) -> Edit.. -> (set the value in the "Default VM arguments:" textbox). You can replace ${HADOOP_HOME} with the path to your Hadoop installation.
You can also start eclipse within a shell.
You export the enronment, before calling eclipse.
Example :
#!/bin/bash
export MY_VAR="ADCA"
export PATH="/home/lala/bin;$PATH"
$ECLIPSE_HOME/eclipse -data $YOUR_WORK_SPACE_PATH
Then you can have multiple instances on eclipse with their own custome environment including workspace.
I was trying to achieve this but in the context of a MAVEN build. As part of my pom.xml configuration, I had a reference to an environment variable as part of a path to a local JAR:
<dependency>
<groupId>the group id</groupId>
<artifactId>the artifact id</artifactId>
<version>the version</version>
<scope>system</scope>
<systemPath>${env.MY_ENV_VARIABLE}/the_local_jar_archive.jar</systemPath>
</dependency>
To compile my project, I had to define the environment variable as part of the run configuration for the maven build as explained by Max's answer. I was able to launch the maven compilation and the project would compile just fine.
However, as this environment variable involves some dependencies, the default "problems" view of Eclipse (where compilation errors/warnings usually show) would still show errors along the lines of Could not find artifact and systemPath should be an absolute path but is ${env.MY_ENV_VARIABLE}/the_local_jar_archive.jar.
How I fixed it
Go into Window -> Preferences -> General -> Worksapce -> Linked Resources and define a new path variable.
Finally, in my case I just needed to Right click on my pom.xml file, select Maven -> Update Project and the errors disappeared from the "Problems" view.
For the people who want to override the Environment Variable of OS in Eclipse project, refer to #MAX answer too.
It's useful when you have release project end eclipse project at the same machine.
The release project can use the OS Environment Variable for test usage and eclipse project can override it for development usage.
I was able to set the env. variables by sourcing (source command inside the shell (ksh) scirpt) the file that was settign them.
Then I called the .ksh script from the external Tools

Eclipse - Showing how eclipse runs project with java.exe

When I run a Java project within the Eclipse IDE, I assume in the background it is simply calling java.exe or javaw.exe with certain parameters, such as the classpath it's using and the actual class it's running.
Is there a way I can make Eclipse show this invocation, with all it's parameters? Ideally I'd like to be able to copy&paste it into a command window to duplicate the running of the project outside of the Eclipse IDE.
(Original Answer: February 2011)
Launch your program in debug mode in Eclipse.
You can then see the exact command in the properties of the Debug views (see this thread or this thread)
First run or debug one of the configurations.
In the Debug view in the Debug perspective, right-click the running process and select "Properties". The command line used to launch the configuration is displayed.
Bug 10820 was about "[java launching] Launch config: show cmd line before launch"... but it is closed as WONTFIX.
Update (March 2012)
bbuser reports in the comment having
had to remove -agentlib:jdwp=transport=dt_socket,suspend=y,address=localhost:60019 from the command line shown in properties in the Debug view.
Otherwise I got connection errors.
Also changing javaw.exe to java.exe might be advisable

How to open and run a specific script from the command line in eclipse

I am trying to run an automated suite every day at the same time, so I want to create a task to open eclipse and execute the main script every day. Is there a way to do this from the command line?
Instead of using eclipse for it, use a software that is dedicated for it - continous integration servers are created for it. Check such titles like: hudson, cruiseControl, TeamCity
You are on the wrong path. Instead of trying to automate opening eclipse, executing a main... break the IDE dependency, write a portable build script using Ant or Maven and execute that build script outside the IDE (using a simple cron job or something more elaborated like a Continuous Integration tool but I'm not sure you need a CI tool for now, start with the build script).
So I am assuming that you want to automate something that you run from inside eclipse. if it's a build then I'm with the other guys that using a build script and CI is the way to go.
But in case it's not that use case...
Now, if you are using the "Run.." dialogs to do this you can actually get the command line paths, binaries and arguments that eclipse used to execute.
What you do is open up the debug perspective. Then run your script however you normally do.
Your Process should appear in the "Debug View" at this point.
Either while the process is running or after termination, right click on the process and open up the properties. (you may need to click 1 level down in the tree to get this option)
Under process info, inside of that there is a section "Command Line". This is the exact command line that eclipse executed behind the scenes to run.
you should be able to put this into a script (.bat for windows / sh for *nix) and schedule accordingly.
edit: added in assumptions, changed to use process info terms which is what is on the properties screen.

Categories

Resources