A
A
Alexander Ivanov2015-01-12 08:50:51
Java
Alexander Ivanov, 2015-01-12 08:50:51

How can I change a class in a jar using maven?

There is a jar without sources. You need to replace the class in it with your class. Can this be done with maven? So far I have found a way to change the class manually (through the archiver).

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nikolai Pavlov, 2015-01-12
@gurinderu

Bad option to change the class in jar
Are you sure that this class has no heirs, static fields and other things?
If not, then forget it.

A
Alexander Ivanov, 2015-01-12
@formatko

Made through antrun plugin.
For the production profile, all the action takes place in the ear module.

<!--Патчим сериализаторы в jar'ках ear'ки для продакш сборки-->
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-antrun-plugin</artifactId>
                        <version>1.6</version>
                        <executions>
                            <execution>
                                <id>repack</id>
                                <phase>package</phase>
                                <goals>
                                    <goal>run</goal>
                                </goals>
                                <configuration>
                                    <target>
                                        <!--разархивируем в спец папку-->
                                        <unzip src="${project.build.directory}/ear${releasePostfix}/lib/gwt-user-${gwt.version}.jar"
                                               dest="${project.build.directory}/ant/user"/>
                                        <unzip src="${project.build.directory}/ear${releasePostfix}/lib/gwt-servlet-${gwt.version}.jar"
                                               dest="${project.build.directory}/ant/servlet"/>
                                        <unzip src="${project.build.directory}/ear${releasePostfix}/lib/iask-api-web-${version}.jar"
                                               dest="${project.build.directory}/ant/apiweb"/>

                                        <!--заменяем нужные классы-->
                                        <copy file="${project.build.directory}/ant/apiweb/com/google/gwt/user/client/rpc/core/java/sql/Date_CustomFieldSerializer.class"
                                              todir="${project.build.directory}/ant/user/com/google/gwt/user/client/rpc/core/java/sql/"
                                              overwrite="true"/>

                                        <copy file="${project.build.directory}/ant/apiweb/com/google/gwt/user/client/rpc/core/java/sql/Date_CustomFieldSerializer.class"
                                              todir="${project.build.directory}/ant/servlet/com/google/gwt/user/client/rpc/core/java/sql/"
                                              overwrite="true"/>

                                        <!--собираем в джарки обратно-->
                                        <zip basedir="${project.build.directory}/ant/user"
                                             destfile="${project.build.directory}/ant/toEar/lib/gwt-user-${gwt.version}.jar"
                                             update="true"/>
                                        <zip basedir="${project.build.directory}/ant/servlet"
                                             destfile="${project.build.directory}/ant/toEar/lib/gwt-servlet-${gwt.version}.jar"
                                             update="true"/>

                                        <!--обновление еарки-->
                                        <zip basedir="${project.build.directory}/ant/toEar/"
                                             destfile="${project.build.directory}/ear${releasePostfix}.ear"
                                             update="true"/>

                                        <delete dir="${project.build.directory}/ant"/>
                                    </target>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>

For the devmode, the replacement occurs in the usual way - creating the same file in the same path (com/google/gwt/user/client/rpc/core/java/sql/Date_CustomFieldSerializer.class) and since the gwt jars get into the application war, manual replacement not required. And for production mode, jars are delivered via provided and cannot be redefined by the code, you have to manually unpack to replace.
There may be a more elegant way

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question