I have a simple problem (maybe not that simple...) for which I couldn't find a solution. I have created a maven project in eclipse and set a line in the pom like this
<name>myTestProject</name>
When I build the project, a myTestProject.jar is correctly generated. Plus, I want my jar to have input parameters, so I created in the project a .bat and a .sh with the line
"java -jar myTestProject.jar input1 input2 ..."
Now, if I want my target name to change along time, in order not to modify the .bat and .sh each time I have changed the lines according to the maven references like this:
"java -jar ${project.name}.jar input1 input2 ..."
But this does not work; the files are just copied but not processed on compilation time, hence neither the .bat nor the .sh will run the jar.
Is there a way to make maven process these files and change the tags for the correct values when compiling?
Thanks in advance,
Miguel.
Related
Question: When running Maven in Eclipse, how do I send the console output to file?
I would like to achieve this using a pom setting or a maven plugin. I do not want to modify the run configurations or the maven system settings.
For reference, I am using Windows 7, Eclipse Luna, Java 6, Maven 3.
As per official command line options you could use -l,--log-file <arg> which provide the:
Log file where all build output will go.
As such, running:
mvn clean install -l output.log
Would not print anything to the console and automatically redirect the whole build output to the output.log file.
If you don't want to type it every time (or you actually don't want to use the command line) and you want it as default option (although rare case I would suppose), you could use new command line options behavior available since version 3.3.1 and have a .mvn folder where the concerned pom.xml file is located and a maven.config file in it simply providing the following line:
-l output.log
That is, the .mvn/maven.config file replaces MAVEN_OPTIONS just for its project, locally where it has been created, with the options it provides, not impacting other builds as per Maven settings of MAVEN_OPTIONS.
This is an IDE agnostic solution (it's a new built-in feature of Maven) and local to a project, but still not provided via simple POM editing, which cannot be achieved since the first phase of Maven default life cycle phases is validate, which:
validate the project is correct and all necessary information is available
That is, during the build, hence when the build has already started (and generated output), it validates the pom.xml file, hence too late to redirect build output at that stage based on some POM properties/plugin.
Go to run as and choose Run Configuration -> Commons -> Select a file.
This should redirect your output to the file you specified.
According to this you can try editing the ${MAVEN_HOME}/conf/logging/simplelogger.properties. I gave it a quick try and maven's output is redirected, but anything else writing to stdout (tests, for instance) still writes on the console
What about creating a fork of M2E and modifying it to read the output file for the launch config from pom.xml
https://github.com/eclipse/m2e-core.git
A possible solution is setting the output format in the mvn file. For example, in the directory /usr/bin, add the desired output informing the path the log will be saved at the end of exec "$JAVACMD" \ line: | tee /home/maven-log.log.
However, it only works when the maven is called by terminal line; when called by IDEs, like eclipse, this solutions does not work.
If I run a jar as follows
java -jar Name.jar arg1 arg2 arg3
it is understandable that when exporting (creating the .jar) I would have had to specify an Eclipse run configuration in order to identify the main method because there may be as many main methods as there are classes.
If I run a main program from within Eclipse, it silently creates a new run configuration which adds more clutter to the run configuration choices available when I want to export. If I do this for many classes, the chances are I have many essentially identical run configurations, with the only difference being the main method specified.
If I run a jar as follows
java -cp Name.jar package.MyObject arg1 arg2
java -cp Name.jar package.MyOtherObject arg1
then the Eclipse run configuration which identifies the main method is probably ignored. The entry point is identified on the command line. In this case, the fact that Eclipse requires a run configuration during the export process, seems to create a chore, but there seems to be no way to avoid this.
I prefer to run my .jar via the -cp option because it gives me access to many entry points. This way I don't have to re-export when I want to access a different entry point. The decision as to which entry point to use is postponed until the time I want to run. I don't have to decide when exporting. I preserve flexibility.
I know an alternative way is to just have an arg1 that specifies the task, so I can rely on just one run configuration that identifies the main method that has a big switch statement and then always invoke Java with the -jar option.
If I use -jar there is the clutter of many run configurations. If I use -cp there seems to be an nuisance step in the export process that involves the clutter of many run configurations. Is there a way to use Eclipse that avoids both of these problems?
Open the project and select File and Export and Java and JAR file. Do not select Runnable JAR file.
By default, every file in the project folder is check-boxed as a resource meant to be exported and you may want to un-check some or all of them. Unfortunately Eclipse does not remember this so you may have to un-check resources every time you are exporting.
Check-box the src directory which unfortunately makes it appear you want to export source but actually you need to check-box it in order to export the corresponding .class files.
Check-box Export generated class files and resources.
Browse to the destination JAR file that you want to export or overwrite.
Click finish.
Eclipse does not consider third-party JAR files to be resources in the above step 2 so you need to find a way to provide them to the -classpath or -cp when you invoke Java. The reason we need to do this is because when Eclipse exports a "JAR file" it doesn't seem to follow the build paths to your third-party JAR files. This is a capability that Eclipse has when it exports a "Runnable JAR file" but that's not what we are doing here. You can manually create a directory of your third-party JAR files and let Java expand a wildcard in -classpath.
Example Java invocation in linux:
java -cp ~/directory/destination.jar:/home/username/directory/thirdparty/"*" com.domain.package.MyObject arg1 arg2 arg3
Note that on linux we can allow bash to expand the ~ for your own JAR file, "destination.jar". For the third-party JAR files the long form /home/username is used because tilde expansion might not work in the middle of the string, just after the colon.
If we want to use the Java * wildcard -- not the same thing as the bash command line wildcard -- for the third-party JAR files, the * character must be quoted (made literal) in order for the wildcard to be passed to Java.
Exporting this way eliminates the need to select a run configuration and addresses the concern that prompted the question.
Aside: If your own JAR is exported to the same directory as the third-party JARs then classpath is simpler:
java -cp ~/singleDirectory/"*" com.domain.package.MyObject arg1 arg2 arg3
I ran it in cmd and it said no main manifest attribute in, JGame2.jar
how do i show it where the main is?? it works fine in eclipse.
For
java -jar x.jar
to work, the jar file must contain a META-INF/MANIFEST.MF file, and this file must contain an entry for the main class. You can add a manifest in Eclipse. Note that any dependencies must also be accounted for, either by:
a class path in the manifest
-cp on the command line
use of some tool to combine to one jar
Eclipse is just an IDE, you might consider learning to use an actual build tool such as Maven or Gradle or Ant to allow you to repeatably build usable results.
As soon as I create a java file in eclipse it creates the corresponding class file in the bin directory which I assume is because eclipse compiles any java file as soon as it is created?Am I right?And later does it compile any java project again when I run it?
If you have Project > Build Automatically checked then Eclipse compiles your Java code as you go.
The code is not compiled again when you Run the program.
You are right for the first part (eg: a *.class in the bin directory).
Eclipse does incremental compilation (eg: almost like compile as you type).
For the second part, I don't understand your question.
It will happen in case Build Automatically option is checked (Menu-->Project-->Build Automatically). If you uncheck that option .class file will not be created when you save java file.
I have one lib/ folder inside my Eclipse Plugin Project and on this folder I have a little script... But when I run the 'Eclipse Plugin Project' I cannot execute that script because I cannot access to that folder...
How can I fix this?
--
Thanks in advance
If you can execute the script by passing an InputStream or a String to the interpreter, put in the src/ folder, so it ends up on the classpath and use getClass().getClassLoader().getResourceAsStream("script-name") to get an InputStream
If the interpreter is external (like bash or something that doesn't implement the Java Scripting API), do the same. When you need to execute the script, create a stream and copy the script to a temporary file.
Keeping the script in lib/ is also a bad idea since the plugin will be assembled into a single JAR file unless you turn that off, so you will end up with a script file inside of a JAR - again something which most interpreters can't use.
By using the classpath, you can let Eclipse figure out where the data is.