I am using exec-maven-plugin to execute a shell script.
<executions>
<execution>
<id>exec-ui-install</id>
<phase>generate-sources</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>bash</executable>
<arguments>
<argument>${basedir}/ui-build.sh</argument>
</arguments>
<skip>${exec.skip}</skip>
</configuration>
</execution>
</executions>
I dont want this to run during build time (as i don't need to run it during build and i am building it from windows). So i am using a parameter named exec.skip and with its help i am able to skip it.
After building jar and moving it to Linux env i am using java command
Ex: java -cp : javaclass
to run the jar. During this i need to execute "exec-maven-plugin" which was disabled during build mode. How do i pass "exec.skip=true" through java command so that i can run plugin.
You cannot do it.
The maven configuration of a project is used during the build of the project.
Once the artifact/component is constructed, you don't interact any longer with maven.
In your case, you should build your component with the suitable configuration parameters before moving it to linux.
Using a Maven profile with this specific configuration that is launched by a continuous integration tool could ease the task and make it reliable.
Related
I've created a simple Micronaut application using
mn create-app app_name --build maven
with a JDK 11 in case that matters.
This creates a maven project which compiles fine, but includes a Dockerfile like this:
FROM adoptopenjdk/openjdk11-openj9:jdk-11.0.1.13-alpine-slim
COPY target/app_name*.jar app_name.jar
EXPOSE 8080
CMD java -XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap -Dcom.sun.management.jmxremote -noverify ${JAVA_OPTS} -jar app_name.jar
However, there is no docker build included in the Maven AFAICT.
So I included this
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>${dockerfile-maven-version}</version>
<executions>
<execution>
<id>default</id>
<goals>
<goal>build</goal>
<goal>push</goal>
</goals>
</execution>
</executions>
<configuration>
<repository>dockerUser/app_name</repository>
<tag>${project.version}</tag>
<buildArgs>
<JAR_FILE>${project.build.finalName}.jar</JAR_FILE>
</buildArgs>
</configuration>
</plugin>
which does manage to build a docker image, but not without manual intervention. The reason is that upon mvn package, three jars get created in target/:
app_name-0.1.jar
app_name-0.1-shaded.jar
original-app_name-0.1.jar
which makes the docker target fail with
When using COPY with more than one source file, the destination must be a directory and end with a /
That message does make sense because all the jars match the COPY source pattern in the Dockerfile.
Right now, I just delete the other two jars (original and shaded) and run the docker target on its own, but that's only fine as long as I work in local manual mode.
Am I missing something or is this an oversight on the Micronaut project creation?
I can't help you with the micronaut configuration, unfortunately. However, if the purpose is to copy the main jar file and the unknown version suffix is the cause of the wildcard being used while copying, a finalName element can be added to the pom.xml in order to strip the version info from the name of the JAR file:
<build>
<finalName>app_name</finalName>
</build>
Am I missing something or is this an oversight on the Micronaut
project creation?
The latter.
If you file an issue at https://github.com/micronaut-projects/micronaut-profiles/issues we can get it straightened out.
Relevant files:
https://github.com/micronaut-projects/micronaut-profiles/blob/c391ef02b5ca087bbdec79f80b129240b29cc246/service/skeleton/maven-build/Dockerfile
https://github.com/micronaut-projects/micronaut-profiles/blob/c391ef02b5ca087bbdec79f80b129240b29cc246/service/skeleton/gradle-build/Dockerfile
Thanks for the input.
I have maven project in which I have defined various .proto files whose corresponding java files are generated through maven plugin. This generated files would be used for implementation [rpc - server implementation], but I want this to be consumed by python client.
Hence need python equivalent to these proto files.
One way is to manually run python protobuf command to these .proto files and generate code, but this would be too manual work. I am looking for some other alternative.
Any help would be appreciated.
You could automate your manual command b running it from Maven. Take a look at the exec-maven-plugin plugin.
You need to add something like that to the plugins part of your pom.xml:
<plugin>
<artifactId>exec-maven-plugin</artifactId>
<groupId>org.codehaus.mojo</groupId>
<executions>
<execution>
<id>Stuff I want done</id>
<phase>generate-sources</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>path/stuff.sh</executable>
</configuration>
</execution>
</executions>
</plugin>
Adapt the phase and the script you want to run.
Worked by adding execution goal as "compile-python" for protobuf-maven-plugin (link) in pom file.
I have a java application which is launched from a shell script: run.sh. The shell script is part of the built java application and it often becomes out of date, or is the wrong version.
So, I write the supported build into the shell script at build time:
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.3</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<file>${basedir}/run.sh.template</file>
<outputFile>${basedir}/run.sh</outputFile>
<replacements>
<replacement>
<token>BUILD_VERSION</token>
<value>${VERSION}B${BUILD_NUMBER}</value>
</replacement>
</replacements>
</configuration>
</plugin>
In the run.sh.template script this does the following:
export VERSION=BUILD_VERSION
This is written to a new file run.sh with the following update:
export VERSION=MYAPP1.9B199
The java application then checks the newly exported VERSION environment variable at run time, to see if it matches the version the application was built with. If they don't match, it issues a warning.
This all works perfectly, but I would like to obfuscate the build number and then decode it back to a real version in java, so it would look like:
export VERSION=FSDHJ£$%(£$"JKSDF
Then in java I would need to be able to decode that string back into the build number. I want to prevent people from hacking the run.sh script to make it work.
Is it possible to execute a Maven plugin from the command line? I need to run dependency plugin:
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${RPTBIN}/.tools/lib</outputDirectory>
<overWriteReleases>false</overWriteReleases>
<overWriteSnapshots>false</overWriteSnapshots>
<overWriteIfNewer>true</overWriteIfNewer>
<excludeTransitive>true</excludeTransitive>
</configuration>
</execution>
</executions>
Is there any way to execute this plugin just like this plugin executes during Maven build?
You should be able to run it with just mvn dependency:copy-dependencies and just add the relevant configuration parameters with -Dparameter=value, i.e. -DoverWriteReleases=false
Yes that is possible. Maven is a Java tool, so you must have Java installed in order to proceed. Please go through the installation process here.
mvn dependency:copy-dependencies
A sample dependency plugin command. By the way how you have been running the Maven command till now?
You can use below command. It worked for me:
mvn {your groupId}:{your artifactId}:{your version}:{your goal}
But remember this command is good if your plugin main class is extending AbstractMojo.
It will run the execute() method of your plugin main class.
Also, before running this command please run
mvn clean install to build the jar
whats the preferred way to upload an artifact via scp to a predefined destination?
i tried using the wagon:upload mojo, but it wont execute atomatically when i defined a "executions" section in my pom like that:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>wagon-maven-plugin</artifactId>
<version>1.0-beta-3</version>
<executions>
<execution>
<phase>release</phase>
<goals>
<goal>upload</goal>
</goals>
</execution>
</executions>
<configuration>
<fromDir>target/checkout/target</fromDir>
<includes>*.jar</includes>
<url>scpexe://host/dir</url>
<toDir />
<serverId>my id</serverId>
</configuration>
</plugin>
i added the necessary extension wagon-ssh and wagon-ssh-external and it all works fine when i execute wagon:upload but it wont upload the artifact automatically in the release phase.
Is this even the right way to upload artifacts to a website, or should the deploy plugin take care of that?
thanks!
That's because no release phase exists (see Maven Lifecycle Reference)
You probably want phase deploy. And yes, wagon is usually used by the maven deploy plugin (automatically when you execute mvn deploy).